có một bài giảng tốt đẹp về điều này từ Google IO 2012 (starting at slide 32)
ở đây tôi Ví dụ chi tiết:
Tạo tệp XML bố cục riêng cho phiên bản ICS + bằng cách đặt nó trong/res/layout-v14. Kết quả là cấu trúc tập tin sẽ giống như thế này:
res/layout
- mainlayout.xml
- compound_button.xml
res/layout-v14
- compound_button.xml
Android sau đó sẽ tìm kiếm các nguồn tài nguyên trong thư mục layout-v14 khi ứng dụng của bạn đang chạy trên v14 hoặc cao hơn.
Nơi một bao gồm trong mainlayout.xml sẽ kéo trong compound_button.xml thích hợp khi ứng dụng được chạy:
<include layout="@layout/compound_button" />
Đối với 4,0 bố trí trước, chúng tôi muốn có một hộp kiểm, vì vậy tạo/layout/compound_button. xml như một hợp nhất như sau:
<?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>
Và sau đó cho 4.0 + bố trí chúng ta muốn có một công tắc, vì vậy tạo /layout-v14/compound_button.xml như một hợp nhất như sau:
<?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>
Tất nhiên, hãy chắc chắn để thiết lập min và mục tiêu của bạn một cách thích hợp:
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="14" />
Xem giải pháp tại đây: http://stackoverflow.com/questions/9920709/use-android-4-0-styled-toggle-button/15640365#15640365 – dberm22