【发布时间】:2015-02-16 07:44:44
【问题描述】:
我发现了一项改进,它可能有助于不将视图值硬编码到我的 android 应用程序中的 java 类中。我想知道我怎样才能让它工作。我想在我的适配器中使用这种视图(这意味着我不能在布局的根目录中使用 @style 属性)。我做错了什么?
代码:
styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="customViewStyleRef">@style/CustomViewStyle</item>
</style>
<attr name="customViewStyleRef" format="reference"/>
<style name="CustomViewStyle" parent="@android:style/Widget.TextView">
<item name="android:layout_margin">10dp</item>
</style>
</resources>
查看类:
public class CustomView extends View {
public CustomView(Context context) {
super(context, null);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs, R.attr.customViewStyleRef);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, R.attr.customViewStyleRef);
}
public void init(@NonNull ViewGroup parentView) {
inflate(getContext(), R.layout.custom_view, parentView);
}
}
视图布局:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f00"
android:textColor="#fff"
android:textSize="50sp"
android:text="test text"/>
</merge>
问题:TextView 容器没有我在 styles.xml 中指定的任何填充(见图)。
【问题讨论】:
标签: android android-view android-styles