Android 向后兼容开关

Android 向后兼容开关,android,Android,ICS有一个开关组件。它满足了我们的需要。2.2有向后兼容的版本吗?找不到任何明显的东西 看起来有人造了这个: 开关仅在4.0上+ 如果你想制作一个使用switch on 4.0+设备的应用程序,你需要做的是声明两个布局。布局中的第一个——v14,将用于ICS设备。在布局文件夹中使用复选框 在代码中,当从开关或复选框获取/设置数据时,请使用CompoundButton类。您会发现CompoundButton在这方面效果很好。如果不可能,您应该使用复选框,如下所述: (幻灯片32)您正在查找此

ICS有一个开关组件。它满足了我们的需要。2.2有向后兼容的版本吗?找不到任何明显的东西


看起来有人造了这个:

开关仅在4.0上+

如果你想制作一个使用switch on 4.0+设备的应用程序,你需要做的是声明两个布局。布局中的第一个——v14,将用于ICS设备。在布局文件夹中使用复选框


在代码中,当从开关或复选框获取/设置数据时,请使用CompoundButton类。您会发现CompoundButton在这方面效果很好。

如果不可能,您应该使用复选框,如下所述:


(幻灯片32)

您正在查找此库:

如果您正在使用holoeverywhere库,您可以在布局文件中使用类似的内容

<org.holoeverywhere.widget.Switch
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
 />

版本
21.0.0
中的Android support AppCompat库包含
Android.support.v7.widget.SwitchCompat
以提供回API v7的兼容性

在gradle中包含如下内容:

compile 'com.android.support:appcompat-v7:21.0.0'
它可用于如下布局:

<android.support.v7.widget.SwitchCompat />


此外,它还具有
showText
属性以简化样式设置-这似乎是native andriod
Switch
中缺少的

要做的第一件事是确保将这些行添加到build.gradle中,然后同步

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
}
其次,创建一个示例活动,在我的例子中,我将其称为SwitchActivity.java

public class SwitchActivity extends ActionBarActivity {

        SwitchCompat mySwitch = null;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_switch);

            // here is your switch
            mySwitch = (SwitchCompat)findViewById(R.id.myswitch);

        }
        .....
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.neoecosystem.samplex.SwitchActivity">

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/myswitch"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

</RelativeLayout>
最后,创建布局,在我的例子中,我将其称为活动\u switch.xml

public class SwitchActivity extends ActionBarActivity {

        SwitchCompat mySwitch = null;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_switch);

            // here is your switch
            mySwitch = (SwitchCompat)findViewById(R.id.myswitch);

        }
        .....
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.neoecosystem.samplex.SwitchActivity">

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/myswitch"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

</RelativeLayout>


我不明白你的答案。我理解文件夹命名约定。我不明白你在说什么。总的来说,答案是“不”,对吗?我想说的是,你可以在ICS上使用开关,但在前置ICS上使用复选框。但是是的,简而言之,答案是“不”。只是确认一下,到今天为止,没有开源兼容性资源。如果我有时间休息,我可以创造一个。我真的很喜欢它的工作方式。link不再工作了。那些呢:我试了第一个,效果很好。代码的质量可能会更好(几乎没有javadoc,有时难以理解,难以修改)。但lib总体上做得很好,提供了可以完全主题化的切换。通过,我们得到了一个很好的结果。我尝试了这两种方法,它们都很好地工作,除了一些设备上的一个严重问题:第一次一个活动主题正确地包含开关,应用程序崩溃,因为找不到其中一个文本属性,导致文本内容的指针为空!如果主题没有设定,是否有人面对这个问题并有解决方案?这应该向项目的github回购问题报告。不在这里。它为我工作,SwitchCompat显示开关在4.x sytle(看起来不难看)像5.x 6.x(看起来不错),它很好地改变开关到SwitchCompat!