自定义ListView项目中的视图没有apptheme

时间:2016-06-25 09:24:20

标签: android android-layout listview themes

问题:

我的listview有一个自定义适配器,目前包含checkboxedittext。我没有更改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>

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

颜色

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

我尝试了什么?

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

这是正常的吗?如果是这样,我该如何更改listitem的样式/主题?如果没有,那可能是什么错误?

我的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;
}

截图:

screenshot checkbox not checked screenshot checkbox checked

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

1 个答案:

答案 0 :(得分:5)

您需要将Activity的{​​{1}}用于需要其主题的样式和属性值的任何内容。由于您的Context Adapter字段已在其构造函数中初始化,因此您需要使用context的{​​{1}}来实例化Adapter,而不是Activity 1}},所以Context最终会得到正确的结果。例如:

Application
相关问题