【问题标题】:Switchcompat not displaying the SwitchSwitchcompat 不显示 Switch
【发布时间】:2014-10-25 15:23:54
【问题描述】:

我正在尝试使用为我的应用程序中的材料设计更新的最新 appcompat 来显示在 Lollipop(http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html) 中显示的开关视图,问题是开关没有显示。我的 SDKmin 为 14,max 为 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>

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'])
}

【问题讨论】:

  • 这是整个布局吗? SwitchCompat 被添加到哪个视图?
  • 不..布局中还有按钮。它在线性布局内。我已经添加了整个布局
  • 对如何让它发挥作用有什么看法吗?
  • 尝试删除app:showText="false" 并使用android:text=""
  • 这不起作用..

标签: android android-5.0-lollipop android-appcompat


【解决方案1】:

我今天遇到了同样的问题,但不知何故它 switchcompat 在我的示例中起作用。我认为应用样式有问题,它的父级应该设置为:

Theme.AppCompat

【讨论】:

  • 这正是我设置的,它不适用于任何前 KitKat 设备。视图根本不显示 - 没有文本,没有开关,什么都没有。
  • 我也有同样的问题!我已将活动主题设置为 Theme.AppCompat(我已经尝试过所有可用的主题,如 Theme.AppComapt.Light.DarkActionBar)不幸的是它不起作用!
  • 需要 v7 21 兼容库的人,如果我没记错的话,仅设置主题是不够的 - 您还需要为新主题设置主要/pr​​imaryDark/accent 颜色。它对我有用。
  • 你是怎么解决这个问题的?我尝试将 Theme.AppCompat 设置为我的 Activity,但我没有看到 Switch 小部件
【解决方案2】:

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

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

【讨论】:

  • 哇……哇。我一直在使用 applicationContext 。用活动上下文来膨胀它对我来说是诀窍。谢谢!
  • 这显然是它......虽然让我很生气,因为它不适合我使用活动上下文...... +1
  • 我浪费了一天的时间,我重复了一整天! @JacksOnF1re 说的也是我的问题。当我初始化适配器时,我传递了getApplicationContext(),现在我将其更改为 MyActivity.this,它就像一个魅力......奇怪的是我做对了并且工作了一段时间,但我做了一些改变在某个时候...
  • 将 getApplicationContext 更改为 MyActivity。这对我有用:)
  • @JacksOnF1re :相同。谷歌就不能在 Docs 中给出一个字吗?我在这上面浪费了几个小时。该死。
【解决方案3】:

或者,您可以使用

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

由您控制

【讨论】:

  • 在绘制其他应用程序时,您也需要设置此项。 FIY
【解决方案4】:

我的 styles.xml 中有一个自定义主题,但我忘记指定其父主题:

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

类似这样的:

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>

【讨论】:

    【解决方案5】:

    好像你遇到过https://code.google.com/p/android/issues/detail?id=78262

    复制布局和png,修复九个补丁,应该没问题。

    【讨论】:

      猜你喜欢
      • 2017-05-05
      • 2016-02-09
      • 2016-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多