0

我有一个列表视图,每个项目都包含一个具有自定义开关样式的 switchcompat 小部件。第一次填充列表视图时,自定义主题未应用于列表中第一项的 switchcompat 小部件。但是第二次填充列表时它工作正常,(列表适配器被清除并重新填充)

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switch_manage_reward_active"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:checked="false"
android:text="active"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#fff"
android:theme="@style/RewardSwitch" />

<style name="RewardSwitch" parent="Theme.AppCompat.Light">
        <item name="colorControlActivated">#E6E329</item>
        <item name="colorSwitchThumbNormal">#f1f1f1</item>
        <item name="android:colorForeground">#ffcccccc</item>
</style>

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder v;
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(R.layout.manage_reward_item, null);
        v = new ViewHolder();
        v.switchRewardStatus = (SwitchCompat) convertView
                .findViewById(R.id.switch_manage_reward_active);
        convertView.setTag(v);
    } else {
        v = (ViewHolder) convertView.getTag();
    }
    final Reward currentReward = getItem(position);
    v.switchRewardStatus.setChecked(currentReward.getActive());
    return convertView;
}

SEE SCREENSHOT switchcompat 列表项

4

2 回答 2

0

null因为你用父参数膨胀

convertView = inflater.inflate(R.layout.manage_reward_item, null);

相反,您应该使用 parent 参数进行膨胀,其中包含有关主题的信息:

convertView = inflater.inflate(R.layout.manage_reward_item, parent, false);
于 2020-10-05T04:53:16.787 回答
0

尝试重新创建活动。我这样做是为了将主题更改为深色主题

于 2020-10-05T04:54:51.827 回答