【问题标题】:Views in Custom ListView item don't have apptheme自定义 ListView 项目中的视图没有 apptheme
【发布时间】:2016-06-25 09:24:19
【问题描述】:

问题:

我的listview 有一个自定义适配器,它当前包含一个checkbox 和一个edittext。我没有更改res/values/styles 文件夹中的任何主题。出于某种原因,checkboxedittext 的风格/主题与普通风格/主题(“AppTheme”)不同。

自动生成的主题:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

但是checkbox是白色的,只有勾选的时候才能看到。 (颜色不是上面设置的colorAccent)并且edittext具有与选中复选框时相同的颜色(绿色/蓝色)。

颜色:

<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>

我尝试了什么?

我尝试使用 xml 属性 android:themestyle 设置项目容器和项目本身的主题/样式。每一个都无济于事。

这正常吗?如果是这样,我该如何更改列表项的样式/主题?如果不是,可能是什么问题?

我的 listitem.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent" >

<CheckBox
    android:id="@+id/cb_listitem"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"/>

<EditText
    android:id="@+id/editText_listitem"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="9"
    android:textColor="@color/black"/>

</LinearLayout>

自定义适配器中的getView:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    if (convertView == null){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.listitem_note_checkbox, null);
        holder = new ViewHolder();
        holder.checkBox = (CheckBox)convertView.findViewById(R.id.cb_listitem);
        holder.editText = (EditText)convertView.findViewById(R.id.editText_listitem);
        convertView.setTag(holder);


    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.editText.setText(items.get(position).note);
    return convertView;
}

截图:

我们可以看到单选按钮具有默认布局,强调色“粉红色”,并且它们具有灰色轮廓。但复选框轮廓为白色,强调色为蓝色/绿色。

【问题讨论】:

  • 如果您没有为您的复选框和编辑文本设置任何样式,它们将默认显示。我在你的问题中遗漏了什么吗?您能否发布一个关于实际情况和预期情况的小屏幕截图?
  • 好的,我一会儿发截图
  • @MikeM。试过了,没用:/
  • 你确定contextActivityContext 吗?你是如何初始化它的?你也可以使用LayoutInflater.from(parent.getContext())
  • 谢谢!您可以发布一个答案,以便我可以接受它

标签: android android-layout listview themes


【解决方案1】:

您需要将ActivityContext 用于需要其主题的样式和属性值的任何内容。由于您的Adaptercontext 字段已在其构造函数中初始化,因此您需要使用ActivityContext 而不是Application 来实例化您的Adapter,因此LayoutInflater 结束找到正确的。例如:

MyAdapter adapter = new MyAdapter(MainActivity.this, list);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    相关资源
    最近更新 更多