2014-04-25 13 views
0

我想在API小于14时在复选框和开关之间进行切换我希望在API超过14时显示复选框我希望切换到出现我尝试了很多导入切换到API小于14,但我不能如果任何人可以帮助我导入切换到API小于14或帮助我做出不同的API布局如果更改API,则在开关和复选框之间进行切换

+0

请发表您的代码,你有什么试过。 –

+0

我不会为make 2布局做任何事情我不知道如何为不同的API版本制作2布局 – mmsmhh

+0

您可以将它们放置在一个布局中,并根据API级别更改它们的可见性。 –

回答

0

您可以通过编程Build.VERSION.SDK_INT检查Build.VERSION.SDK_INT作为如下

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 
    // show Switch 
} else { 
    // show CheckBox 
} 

或者你也可以针对不同的API级别创建不同的布局XML,并付诸相应的布局文件夹,如下

res/layout/mylayout.xml  (API Level 8+) 
res/layout-v11/mylayout.xml (API Level 11+) 
res/layout-v14/mylayout.xml (API Level 14+) 
+0

此错误出现在XML查看需要API级别14(当前最小值为9): mmsmhh

+0

我已更新我的答案。 –

+0

我不明白的更新,你可以讨论它 – mmsmhh

0

使用资源文件夹使用CheckBox默认和Switch的API级别14以上。

res/layout/compound_button.xml

<CheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

res/layout-v14/compound_button.xml

<Switch 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

然后,您可以包括其他布局compound_button,说activity_main.xml

<include 
    android:id="@+id/turbo" 
    layout="@layout/compound_button" /> 

由于两个CheckBoxSwitch延伸CompoundButton,你可以在Java代码中这样的值:

CompoundButton turbo = (CompoundButton) findViewById(R.id.turbo); 
if (!turbo.isChecked()) { 
    // Slow down the computer 
} 

关注我更多的信息,请与(在21:25开始): http://www.infoq.com/presentations/android-fragmentation-myth

+0

我不明白这个答案 – mmsmhh

+0

不应该'res/layout-v14/compound_button.xml' – Raghunandan

+0

好赶上!固定。 – chiuki

相关问题