我正在使用自定义列表适配器实现和应用。当我膨胀每个具有定义样式的列表项时,该样式似乎不起作用。 我调用膨胀这样做:
convertView = inflater.inflate(R.layout.widget,null);
布局是这样的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?widgetWrapperStyle">
<LinearLayout
style="@style/stat" />
<RelativeLayout
style="?widgetStyle"
android:layout_below="@+id/widgetStat" >
<TextView
android:id="@+id/head"/>
<LinearLayout
android:id="@+id/body" />
</RelativeLayout>
我的 attrs.xml 和 style.xml 是:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="widgetWrapper">
<attr name="widgetWrapperStyle" format="reference" />
</declare-styleable>
<declare-styleable name="widget">
<attr name="widgetStyle" format="reference" />
</declare-styleable>
</resources>
<style name="AppThemeDark" parent="AppBaseThemeDark">
<item name="widgetWrapperStyle">@style/widgetWrapperDark</item>
<item name="widgetStyle">@style/widget</item>
</style>
<style name="widgetWrapperDark" parent="widgetWrapper">
<item name="android:background">@color/list_item_background_dark</item>
</style>
<style name="widget">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:padding">@dimen/widget_padding</item>
</style>
'style="?xxxx"' 似乎都没有用。当 View 膨胀时,背景颜色不正确。
最佳答案
在展开布局时,使用正确配置的 LayoutInflater
很重要。特别是,它需要从 Activity
创建,理想情况下只需调用 getLayoutInflater()
(或 getSupportLayoutInflater()
来创建 SherlockActivity
和 kin,IIRC)。
戴夫史密斯有 an excellent blog post查看各种类型的 Context
,包括涉及主题和布局膨胀的问题。
关于android - 使用定义的样式膨胀布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17428222/