【问题标题】:Inflate layout with defined style定义样式的膨胀布局
【发布时间】:2013-07-02 22:50:45
【问题描述】:

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

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"' 似乎都没有工作。当视图膨胀时,背景颜色不是正确的。

【问题讨论】:

  • 你是如何创建inflater的?
  • 这样做:inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);而上下文就是活动上下文。
  • 请尝试在 Activity 上调用 getLayoutInflater(),以确保安全。
  • 哦!伟大的!谢谢,这解决了我的问题。

标签: android listview android-inflate


【解决方案1】:

在扩展布局时,使用正确配置的LayoutInflater 很重要。特别是,它需要从Activity 创建,理想情况下只需调用getLayoutInflater()(或getSupportLayoutInflater() 用于SherlockActivity 和亲属,IIRC)。

Dave Smith 让an excellent blog post 审查各种类型的Context,包括涉及主题和布局膨胀的问题。

【讨论】:

  • 谢谢!如此简单和合乎逻辑,当我知道时:D!
  • 另外,LayoutInflater inflater = LayoutInflater.from(context);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-17
相关资源
最近更新 更多