android - 自定义 ListView 项目中的 View 没有应用主题

标签 android android-layout listview themes

问题:

我有一个用于我的 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)并且编辑文本的颜色与复选框选中时的颜色相同(绿色/蓝色)。

颜色:

<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>

在自定义适配器中获取 View :

@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;
}

截图:

screenshot checkbox not checked screenshot checkbox checked

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

最佳答案

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

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

关于android - 自定义 ListView 项目中的 View 没有应用主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38026932/

相关文章:

android - Gradle 和 Android : how to use the latest available build tools?

android - 使服务关闭外部 Activity (通过隐式 Intent 创建)

android - 在按钮单击时 react native 水平滚动

android - 在 DrawerLayout 内的 NavigationView 顶部布局图像

android - 无法通过 ListView 检测手势

java - 如何使用之前添加的值填充 ListView 编辑文本?

android - 单个开发者可以在 Android Market 上发布多少个不同的应用程序是否有限制?

android - 适用于 8 英寸平板电脑的可绘制文件夹

android - 如何使用自定义 View 创建通知,但具有原生外观?

android - 软键盘隐藏Fragment中的ListView元素