2015-02-09 57 views
0
內部屬性

我知道,我可以訪問控件這樣,當我創建的自定義部件的自定義屬性:Accesing widget的

TypedArray a = context.getTheme().obtainStyledAttributes(
       attributeSet, 
       R.styleable.FormEditText, 
       0, 0); 
     try { 
      leftIcon = a.getDrawable(R.styleable.FormEditText_leftIcon); 
     } finally { 
      a.recycle(); 
     } 

但是,如果我想訪問android:hint,我怎麼會提到它的時候創建一個新的自定義視圖?

謝謝!

回答

1

如果要訪問現有android:hint屬性,在自定義視圖,你只需要創建一個新的設置樣式並參考現有的Android atrribute:

attrs.xml:

<declare-styleable name="YourView"> 
    <attr name="android:hint" /> 
</declare-styleable> 

在你的customView初始化你可以得到如下的屬性:

TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.YourView); 
CharSequence hint = a.getText(R.styleable.YourView_android_hint) 
a.recycle();