【问题标题】:custom theme not applied on first SwitchCompat item on android listview自定义主题未应用于 android listview 上的第一个 SwitchCompat 项目
【发布时间】:2020-10-05 04:35:35
【问题描述】:

我有一个列表视图,其中每个项目都包含一个具有自定义开关样式的 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 list item

【问题讨论】:

    标签: android switchcompat


    【解决方案1】:

    因为你用null父参数膨胀

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

    相反,您应该使用包含主题信息的父参数进行膨胀:

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

    【讨论】:

      【解决方案2】:

      尝试recreate 活动。我这样做是为了将主题更改为深色主题

      【讨论】:

      • 我什么时候应该调用 recreate()?
      • 切换主题后
      猜你喜欢
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 2010-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-02
      相关资源
      最近更新 更多