android - 使用定义的样式膨胀布局

标签 android listview android-inflate

我正在使用自定义列表适配器实现和应用。当我膨胀每个具有定义样式的列表项时,该样式似乎不起作用。 我调用膨胀这样做:

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/

相关文章:

android相机预览回调正在非常缓慢地绘制叠加 View

android - 使用 Google Play 应用内结算的一次性购买的时间试用

java - WebView 未从 ListView 按钮加载正确的 URL

C# 从 ListView 外部的控件中删除 ListView 中的项目

java - 另一个 ExpandableView 内的 ExpandableView 不会使子元素膨胀

android - 无法动态膨胀Fragment

android - Android动态添加EditText、Spinner、Radiobutton

android - 单例被摧毁

java - 从 SQLite 数据库加载数据到 ListView

android - viewPager 背景颜色对我的图像的影响