如何在样式文件中定义可绘制对象的大小? [英] How can I define the size of the drawables in the style file?
问题描述
我只想在 res/values/styles.xml 中一次指定两个复选框的大小及其四个形状可绘制对象
I would like to specify just once in res/values/styles.xml the size of two checkboxes and their four shape drawables
- res/drawable/cb1_checked.xml
- res/drawable/cb1_unchecked.xml
- res/drawable/cb2_checked.xml
- res/drawable/cb2_unchecked.xml .
- res/drawable/cb1_checked.xml
- res/drawable/cb1_unchecked.xml
- res/drawable/cb2_checked.xml
- res/drawable/cb2_unchecked.xml.
通过这种方式,尺寸将在样式中显示一次,而不是在可绘制区域中显示四次.
This way the size would appear one time in the style rather than four in the drawables.
以下两种尝试均无效.您能提出解决方案吗? (如果您发现这两次尝试出了什么问题,请务必提及.)
Neither of the two attempts below work. Can you suggest a solution? (If you see what's wrong with these two attempts, please do mention it.)
--------------------------------------------------- ----------------- 尝试1 ------------------------ ----------------------------------------
----------------------------------------------------------------Attempt 1----------------------------------------------------------------
替换
res/drawable/cb1_checked.xml
<size android:width="24dp"
android:height="24dp" />
使用
res/values/styles.xml
<item name="android:layout_width">24dp</item>
<item name="android:layout_height">24dp</item>
--------------------------------------------------- ----------------- 尝试2 ------------------------ ----------------------------------------
----------------------------------------------------------------Attempt 2----------------------------------------------------------------
添加
res/values/styles.xml
<item name="android:background">cb_background</item>
并定义背景可绘制对象
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>
--------------------------------------------------- ----------------- 已添加 ------------------------- ---------------------------------------
----------------------------------------------------------------Added----------------------------------------------------------------
这是完整的XML文件集:
Here is the full set of XML files:
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/styles.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>
(以及类似的cb2选择器和选中/未选中的可绘制对象)
(and similarly for the cb2 selector and checked/unchecked drawables)
推荐答案
有一个很弱的解决方案,即 android:width 和 android:height 出现四次,但至少可以通过仅设置一个参数来调整尺寸.
There is a weak solution, weak in the sense that android:width and android:height still appear four times, but at least the dimension can be adjusted by setting just one parameter.
对于四个可绘制对象,分别添加
For each of the four drawables, add
<size android:width="@dimen/my24dp"
android:height="@dimen/my24dp" />
并定义尺寸
res/values/dimens.xml
<dimen name="my24dp">24dp</dimen>
所以问题仍然存在:可绘制对象中是否完全不指定尺寸?
So the question remains: Can the size not be specified at all in the drawables?
这篇关于如何在样式文件中定义可绘制对象的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!