Tôi muốn chỉ định chỉ một lần trong res/values / styles.xml kích thước của hai hộp kiểm và bốn hình dạng của chúng drawablesLàm cách nào để xác định kích thước của các đối tượng có thể kéo trong tệp kiểu?
- res/drawable/cb1_checked.xml
- res/drawable/cb1_unchecked.xml
- res/drawable/cb2_checked.xml
- res/drawable/cb2_unchecked.xml.
Bằng cách này, kích thước sẽ xuất hiện một lần theo kiểu thay vì bốn lần trong bản vẽ.
Cả hai thử nghiệm dưới đây đều không hoạt động. Bạn có thể đề xuất một giải pháp? (Nếu bạn thấy có vấn đề gì với hai lần thử này, vui lòng đề cập đến nó.)
----------------------------- ----------------------------------- Nỗ lực 1 ----------- -------------------------------------------------- ---
Thay
res/drawable/cb1_checked.xml
<size android:width="24dp"
android:height="24dp" />
với
012.351.res/values / styles.xml
<item name="android:layout_width">24dp</item>
<item name="android:layout_height">24dp</item>
------------------------------- --------------------------------- Nỗ lực 2 ------------- -------------------------------------------------- -
Thêm
res/values / styles.xml
<item name="android:background">cb_background</item>
và xác định một nền drawable
res/drawable/cb_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#00FF00"/>
<size android:width="24dp"
android:height="24dp" />
</shape>
-------------------- -------------------------------------------- Đã thêm --- -------------------------------------------------- -----------
Đây là tập hợp đầy đủ các tệp XML:
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MyLL"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/cb1"
style="@style/CB_style"
android:button="@drawable/cb1_selector"
android:height="@dimen/my24sp" />
<CheckBox
android:id="@+id/cb2"
style="@style/CB_style"
android:button="@drawable/cb2_selector"
android:height="@dimen/my24sp" />
</LinearLayout>
res/values / phong cách.xml
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
<style name="CB_style" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:gravity">center</item>
<item name="android:checked">false</item>
</style>
res/drawable/cb1_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true">
<item android:state_checked="true"
android:drawable="@drawable/cb1_checked" />
<item android:state_checked="false"
android:drawable="@drawable/cb1_unchecked" />
</selector>
res/drawable/cb1_checked.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FF0000"/>
<size android:width="24dp"
android:height="24dp" />
</shape>
res/drawable/cb1_unchecked.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#880000"/>
<size android:width="24dp"
android:height="24dp" />
</shape>
(và tương tự cho bộ chọn CB2 và kiểm tra/kiểm soát drawables)
Tôi đang làm điều tương tự và câu trả lời này hoạt động. OP nên chấp nhận câu trả lời này. –
Tôi đã làm điều tương tự nhưng nó không hoạt động. Tôi có lỗi sau: Lỗi: Lỗi: Chiều rộng (0) và chiều cao (0) không được <= 0'. Tôi có thị trường skd min thiết lập để 19. – roroinpho21
@ roroinpho21 Nghe có vẻ đơn giản, nhưng bất cứ điều gì người ta có thể đề xuất ở đây sẽ chỉ là một phỏng đoán. Kiểm tra kỹ và đăng câu hỏi. – Calaf