【发布时间】:2014-06-12 10:43:09
【问题描述】:
我创建了一个自定义复合视图 - 视图加载,没有崩溃,到目前为止一切顺利。
现在,我计划在我的应用程序中多次使用这个视图,所以这个视图需要一个样式。
我在 attr.xml 文件中为它声明了一个样式
<declare-styleable name="MyCustomView">
<attr name="ff_label" format="string" />
<attr name="ff_fieldText" format="string" />
</declare-styleable>
<declare-styleable name="MyCustomViewStyle">
<attr name="customViewStyle" format="reference" />
</declare-styleable>
然后我去我的主题文件里面写了这个
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
//bunch of other stuff
<item name="customViewStyle">@style/customViewStyle</item>
</style>
然后在我的android清单中我声明了
<application
android:name="com.my.app.App"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
然后在我的 styles.xml 文件中我写了
<style name="customViewStyleStyle" parent="@android:style/Widget.EditText">
<item name="android:paddingBottom">@dimen/space_between_adjacent_widgets_vertical</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="ff_label">@string/default_label_text</item>
<item name="ff_fieldText">@string/default_label_text</item>
</style>
我的问题:我的 OWN 属性被识别得很好。
为什么标记为"android:..." 的属性会被忽略?
MyCustomView.java
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttributes(context, attrs, R.attr.customViewStyle);
}
public MyCustomView(Context context) {
super(context);
initAttributes(context, null, R.attr.customViewStyle);
}
private void initAttributes(Context context, AttributeSet attrs, int defStyle) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_view, this, true);
label = (TextView) findViewById(R.id.label);
formField = (EditText) findViewById(R.id.formField);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyle,0);
if (a.hasValue(R.styleable.MyCustomView_ff_label)) {
labelText = a.getString(R.styleable.MyCustomView_ff_label);
label.setText(labelText);
}
if (a.hasValue(R.styleable.MyCustomView_ff_fieldText)) {
fieldText = a.getString(R.styleable.MyCustomView_ff_fieldText);
field.setHint(fieldText);
}
a.recycle();
}
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.my.app"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.my.app.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.my.app.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.my.app.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.my.app.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.my.app.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.my.app.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.my.app.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.my.app.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<include layout="@layout/layout_set_default" />
</TableLayout>
1 视图的默认高度约为 30 dp - > 我不能为它使用列表视图。 每个视图之间应该有 10dp 的填充
【问题讨论】:
标签: android android-custom-view android-styles