kotlin-如何动态更改switchcompat样式

rjjhvcjd  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(500)

我有一个 <androidx.appcompat.widget.SwitchCompat 我需要改变 theme 使用kotlin编程的属性。
下面是我的自定义开关:

  1. <androidx.appcompat.widget.SwitchCompat
  2. android:id="@+id/switch_me"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:text="@string/some_text"
  6. android:textColor="@color/white"
  7. android:theme="@style/SwitchTheme1" />

这是我的两种不同风格:

  1. <style name="SwitchTheme1" parent="ThemeOverlay.AppCompat.Light">
  2. <item name="colorControlActivated">@color/white</item>
  3. <item name="trackTint">@color/colorPrimary1</item>
  4. </style>
  5. <style name="SwitchTheme2" parent="ThemeOverlay.AppCompat.Light">
  6. <item name="colorControlActivated">@color/white</item>
  7. <item name="trackTint">@color/colorPrimary2</item>
  8. </style>

我只需要准备一下 SwitchTheme1 或者 SwitchTheme2 根据具体情况使用以下代码:

  1. override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? {
  2. val view = inflater.inflate(R.layout.fragment_settings, container, false)
  3. if(some_condition == "my value"){
  4. /*Set SwitchTheme1*/
  5. }else{
  6. /*Set SwitchTheme2*/
  7. }
  8. return view
  9. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题