Switchcompat not displaying the Switch

前端 未结 5 1817
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-16 12:15

I am trying to use the latest appcompat which was updated for material design in my app to show the switch view as displayed in Lollipop(http://android-developers.blogspot.i

相关标签:
5条回答
  • 2020-12-16 13:04

    I had same problem today but somehow it switchcompat worked in my sample. I think that there is problem with app style, it parent should be set to:

    Theme.AppCompat
    
    0 讨论(0)
  • 2020-12-16 13:09

    I'm not sure if this is a bug in the support library, but you have to ensure that the context for your layout inflater is a themed one.

    1. Make sure your activities theme Theme.AppCompat as parent
    2. If you use inflate SwitchCompat in ListView or RecyclerView you have to ensure that the LayoutInflater you instantiate and use in your adapter is created with the themed context. You can retrieve the themed context by calling: Activity.getSupportActionBar().getThemedContext()
    0 讨论(0)
  • 2020-12-16 13:11

    Alternatively, you can utilize

    android:theme="@style/Theme.AppCompat"

    On your control

    0 讨论(0)
  • 2020-12-16 13:11

    I had a custom theme in my styles.xml and I forgot to specify its parent:

    name="CustomTheme.switchState" parent="Theme.AppCompat.Light"

    Something like this:

    custom_linear_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    
    <androidx.appcompat.widget.SwitchCompat
        android:id="@+id/switch1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:text="Hello"
        android:theme="@style/CustomTheme.switchState" />
    
    ...
    </LinearLayout>
    

    style.xml

    <resources>
    ...
    
    name="CustomTheme.switchState" parent="Theme.AppCompat.Light">
            <item name="colorControlActivated">@color/colorOne</item>
            <item name="android:textOn">On</item>
            <item name="android:textOff">Off</item>
        </style>
    
    ...
    </resources>
    
    0 讨论(0)
  • 2020-12-16 13:18

    Seems like you've encountered https://code.google.com/p/android/issues/detail?id=78262

    Copy the layout and pngs, fix the nine-patch, and you should be fine.

    0 讨论(0)
提交回复
热议问题