2012-07-11 83 views
15

在我的項目,我不能插入由於以下錯誤的開關:Android:需要使用Switch的API嗎?

查看需要API等級14(當前分鐘8):

但在我的項目屬性,我用平臺4.1和API級別16.那麼,哪裏出了問題?在AndroidManifest.xml文件

+0

看看這裏的解決方案:http://stackoverflow.com/questions/9920709/use-android-4-0-styled-toggle-button/15640365#15640365 – dberm22 2013-03-26 16:53:47

回答

74

有關於這一個很好的演講從Google IO 2012 (starting at slide 32)

在這裏,我詳細示例如下:

將其放置在/ res/layout-v14中,爲ICS +版本創建單獨的佈局XML文件。得到的文件結構將是這個樣子:然後

res/layout 
- mainlayout.xml 
- compound_button.xml 
res/layout-v14 
- compound_button.xml 

Android將尋找資源的佈局V14目錄時,你的應用是在V14或更高版本上運行。

發佈包括mainlayout.xml運行應用程序時,將在相關compound_button.xml拉:

<include layout="@layout/compound_button" /> 

我們想要一個複選框前4.0的佈局,所以創建/配置/ compound_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+佈局,所以創建/layout-v14/compound_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" /> 
+0

請提供該鏈接內容的摘要。請不要求我們轉到另一個網站,只是爲了從您的答案中受益。而如果其他網站消失或鏈接中斷? – 2012-10-15 18:57:47

+0

交換機僅適用於API v14及更高版本。所以我猜應該使用layout-v14而不是layout-v11? – Almer 2012-12-13 15:33:43

+0

你是對的,阿爾默。開關直到v14才引入,所以應該使用layout-v14。好的趕上! – suomi35 2012-12-30 04:51:56

5

尋找此:

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"/> 

變化的minSdkVersion至14

+0

謝謝你!我不知道它是在Manifest中。 – Rob 2012-07-11 14:38:30

+1

請注意,如果您將minSdkVersion更改爲14,則您的應用將只適用於Android 4.0及更高版本的手機。目前只有約10%的Android手機。 – azgolfer 2012-07-11 14:44:15

+0

我明白了。那麼什麼是Switch的最佳選擇?我想我將不得不使用CheckBox。 – Rob 2012-07-11 14:46:23

1

android:minSdkVersion爲8。這意味着您的應用將API-8(2.2中運行。 1)及以上,但它會崩潰,因爲開關不可

1

一個更好的解決方案是在佈局文件夾和佈局-V14夾兩種不同的XML佈局有相同的名字和相同的代碼。在佈局中,它的toggleButton /複選框和佈局-v14其開關並動態檢查api版本並擴充相應的佈局。

0

看有兩種方法首先要解決你的問題,或者您從8改變你的minSdkVersion <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"/> 14或定義佈局-V14另一個包含開關佈局,可以手動創建資源目錄此文件夾

3

開關需要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"); 
      } 
     } 
    }); 

我希望能幫到你。