2011-08-08 64 views
0

我創建了一個包含TextView和一個擴展了LinearLayout(稱爲LabeledSpinner)的Spinner的自定義佈局。 我把這些LabeledSpinners中的幾個放入我的主RelativeLayout中,顯然我想爲每個textview設置不同的文本,但我不知道如何在xml文件中執行它。訪問自定義佈局的內部視圖的屬性

我需要以某種方式訪問​​內部視圖的屬性,請幫助。 我試圖用[包名] .LabeledSpinner.label:文本,但它沒有工作

(標籤是的TextView的id)

回答

1

讓LabeledSpinner一個設置樣式(RES /價值/ attrs.xml )

<declare-styleable name="LabeledSpinner"> 
    <attr name="text" format="string"/> 
</declare-styleable> 

在你LabeledSpinner

public LabeledSpinner(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LabeledSpinner); 
    String text = a.getString(R.styleable.LabeledSpinner_text); 
    a.recycle(); 
} 

注意,在LabeledSpinnertext屬性是用作LabeledSpinner_text

現在,在您的佈局

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:yourpackage="http://schemas.android.com/apk/res/com.package"> 
    <com.package.LabeledSpinner 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     yourpackage:text="This is where your text goes"/> 
</LinearLayout> 

這應該做的!