我已經創建了一個包含RelativeLayout
一個簡單的自定義視圖自定義視圖得到的Android默認屬性和EditText
:如何
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edt_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
而且我已經添加在res/values/attrs.xml
一些自定義的屬性和我檢索這些屬性我的自定義視圖構造函數和一切正常。
現在,我想在我的自定義視圖中檢索EditText
默認屬性,例如我想在我的自定義視圖中獲取android:text
屬性。
我的自定義視圖類(簡化):
public class CustomEditText extends RelativeLayout {
private int panCount;
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.CustomEditText, 0, 0);
try {
this.panCount = typedArray.getInt(R.styleable.CustomEditText_panCount, 16);
} finally {
typedArray.recycle();
}
}
}
我怎麼能做到這一點,而不在res/values/attrs.xml
重新聲明文本屬性?
它是混淆你不使用你的觀點在XML代碼...!你確定你已經發布了最新的代碼。檢查這個答案:https://stackoverflow.com/a/8090772/7134908它可能會解決您的問題,在xml中設置值,並進入Java vode –
你可以添加'Customable文件''CustomEditText'? – tynn
[如何在自定義TextView中使用命名空間「android」獲取屬性的可能的重複](https://stackoverflow.com/questions/26486791/how-to-get-attributes-with-namespace-android-in-custom-textview ) –