Switchcompat not displaying the Switch

2019-01-24 01:44发布

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.in/2014/10/appcompat-v21-material-design-for-pre.html), the problem is the switch is not getting displayed. My SDKmin is 14 and max is 21. I am using the below code in the layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/fragment_scheduler"
    tools:context="com.stackoverflow.ranjith.androidprojdel.SchedulerUI">

<android.support.v7.widget.SwitchCompat
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="switch"
        app:showText="false" />

    <Button
        android:layout_width="match_parent"
        android:text="start"
        android:id="@+id/start"
        android:layout_height="wrap_content" />
</LinearLayout>

Build.gradle:

dependencies {
    compile 'com.android.support:cardview-v7:+'
    compile 'com.android.support:recyclerview-v7:+'
    compile 'com.android.support:palette-v7:+'
    compile 'com.android.support:support-v4:+'
    compile "com.android.support:appcompat-v7:21.0.+"
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

4条回答
Melony?
2楼-- · 2019-01-24 02:02

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.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-24 02:07

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
查看更多
甜甜的少女心
4楼-- · 2019-01-24 02:21

Alternatively, you can utilize

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

On your control

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-24 02:25

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()
查看更多
登录 后发表回答