使TextView元件的android:ellipsize="marquee"屬性生效

我叫阿狸貓發表於2014-02-27

由於TextView預設情況下是獲取不到焦點的,即便設定android:focusable="true",也是沒有獲取到焦點的。

解決辦法:自定義UI

1.建立一個類繼承TextView,實現3個構造方法。

2.覆寫isFocused()方法,讓它的返回值為true。(這樣就能欺騙系統,自定義的控制元件也就能獲取到焦點了)

3.在佈局檔案中不要使用TextView,而是使用自定義類(全路徑)。


自定義UI

package com.xxc.mobilesafe.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class FocusedTextView extends TextView {

	public FocusedTextView(Context context) {
		super(context);
	}

	public FocusedTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public FocusedTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	/**
	 * 欺騙系統,讓系統認為FocusedTextView得到了焦點了
	 */
	public boolean isFocused() {
		return true;
	}
}

佈局檔案

<com.xxc.mobilesafe.ui.FocusedTextView 
        android:focusable="true"
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:singleLine="true"
    	android:ellipsize="marquee"
    	android:text="123456789asdasdasofieowijfof8dureu04nrf"
/>




相關文章