由于以下错误,我无法在项目中插入开关:
视图需要接口级别14 (当前最小为8):
但是在我的项目属性中,我使用的是Platform 4.1和API Level 16。
发布于 2012-10-16 02:50:12
Google IO 2012 (starting at slide 32)上有一个关于这方面的很好的讲座
下面是一个详细的例子:
通过将其放置在/res/ ICS+ -v14中,为布局版本创建一个单独的布局XML文件。生成的文件结构如下所示:
res/layout
- mainlayout.xml
- compound_button.xml
res/layout-v14
- compound_button.xml
当你的应用在v14或更高版本上运行时,安卓会在layout-v14目录中查找资源。
在mainlayout.xml中放置一个include,它将在应用程序运行时拉入相关的compound_button.xml:
<include layout="@layout/compound_button" />
对于4.0之前的布局,我们需要一个复选框,因此可以按如下方式创建/ layout /复合_button.xml作为合并:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<CheckBox
android:id="@+id/enabled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enable" />
</merge>
然后,对于布局布局,我们需要一个开关,因此创建/ 4.0+ -v14/复合_Button.xml作为合并,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<merge 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" >
<Switch
android:id="@+id/enabled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enable"
tools:ignore="NewApi" />
</merge>
当然,一定要适当地设置你的最小值和目标:
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="14" />
发布于 2012-07-11 22:36:39
在您的AndroidManifest.xml文件中查找以下内容:
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"/>
将minSdkVersion更改为14。
发布于 2016-05-23 16:53:38
交换机需要API 14,旧版本请使用SwithCompat:
<android.support.v7.widget.SwitchCompat
android:id="@+id/switch_compat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:checked="true"
android:textOff="OFF"
android:textOn="ON"
app:showText="false"
android:focusable="false"
android:focusableInTouchMode="false"/>
在setOnCheckedChangeListener上:
witchCompat switchCompat = (SwitchCompat)convertView.findViewById(R.id.switch_compat);
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
textView.setText("check");
} else {
textView.setText("unCheck");
}
}
});
希望能对你有所帮助。
https://stackoverflow.com/questions/11435001
复制相似问题