Switchcompat没有显示Switch

Psy*_*her 18 android android-appcompat android-5.0-lollipop

我正在尝试使用我的应用程序中为材料设计更新的最新appcompat,以显示Lollipop中显示的切换视图(http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design -for-pre.html),问题是交换机没有显示出来.我的SDKmin是14,最大值是21.我在布局中使用以下代码:

<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>
Run Code Online (Sandbox Code Playgroud)

的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'])
}
Run Code Online (Sandbox Code Playgroud)

soc*_*qwe 24

我不确定这是否是支持库中的错误,但您必须确保布局inflater的上下文是主题.

  1. 确保您的活动主题Theme.AppCompat为父级
  2. 如果在ListView或RecyclerView中使用inflate SwitchCompat,则必须确保使用主题上下文创建在适配器中实例化和使用的LayoutInflater.您可以通过调用以下方法检索主题上下文:Activity.getSupportActionBar().getThemedContext()

  • 哇...哇哇.我使用了applicationContext,因为我一直在使用它.用活动上下文来膨胀它对我来说是个窍门.谢谢! (3认同)
  • 我爱你sockeqwe. (2认同)

小智 21

我今天遇到了同样的问题,但不知何故,switchcompat在我的样本中工作.我认为app风格有问题,它应该设置为:

Theme.AppCompat
Run Code Online (Sandbox Code Playgroud)

  • 这正是我设置的,它不适用于任何pre-KitKat设备.视图根本没有显示 - 没有文字,没有开关,没有. (4认同)
  • 我也有同样的问题!我已将活动主题设置为 Theme.AppCompat(我已经尝试了所有可用的主题,例如 Theme.AppCompapt.Light.DarkActionBar)不幸的是它不起作用! (2认同)
  • 你是怎么解决这个问题的?我尝试将 Theme.AppCompat 设置为我的 Activity 但我没有看到 Switch 小部件 (2认同)

Ale*_*art 7

或者,您可以利用

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

由您掌控