我该如何在样式文件中定义可绘制对象的大小?

8
我希望能够在 res/values/styles.xml 文件中仅指定两个复选框的大小和它们的四个形状绘制。这样一来,大小只需在样式中出现一次,而不是在四个可绘制对象中各出现一次。
以下是需要修改的文件列表:
  1. res/drawable/cb1_checked.xml
  2. res/drawable/cb1_unchecked.xml
  3. res/drawable/cb2_checked.xml
  4. res/drawable/cb2_unchecked.xml
下面是两种尝试但均未成功的方法,请您提供一个解决方案。(如果你看到了这两种方法的问题,请务必提出。)

----------------------------------------------------------------尝试 1----------------------------------------------------------------

将:

res/drawable/cb1_checked.xml

<size android:width="24dp"
      android:height="24dp" />

with

res/values/styles.xml

<item name="android:layout_width">24dp</item>
<item name="android:layout_height">24dp</item>

---------------------------------------------------------------尝试 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>

----------------------------------------------------------------新增内容----------------------------------------------------------------

以下是完整的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/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选择器和选中/未选中的可绘制对象同样适用)
2个回答

8

有一个较弱的解决方案,即在android:widthandroid:height仍然出现四次,但至少只需设置一个参数即可调整维度。

对于这四个可绘制对象中的每一个,都添加以下内容:

<size android:width="@dimen/my24dp"
      android:height="@dimen/my24dp" />

同时也需要定义维度

res/values/dimens.xml

<dimen name="my24dp">24dp</dimen>

那么问题仍然存在:在可绘制对象中根本不能指定大小吗?

我正在做完全相同的事情,而且这个答案是有效的。OP 应该接受这个答案。 - Toby Wilson
我做了同样的事情,但它没有起作用。我遇到了以下错误 Error:Error: Width (0) and height (0) cannot be <= 0。我的最小SDK目标设置为19。 - roroinpho21
@roroinpho21 这听起来很简单,但是任何人在这里提出的建议都只是猜测。请再次检查并发布一个问题。 - Calaf

0

你可以将尺寸存储为资源在res/values/dimens.xml中,例如:

<resources>
    <dimen name="checkbox_width">24dp</dimen>
</resources>

然后将这个值放入您的布局中,如下所示:

<CheckBox
        android:layout_width="@dimen/checkbox_width"
        android:layout_height="@dimen/checkbox_width" />

真的吗?出了什么错误?在创建新的dimens.xml文件后,您应该清理项目。然后Java将创建引用ID以使用@dimen/checkbox_width获取值。 - longi
没有错误。在Eclipse或设备上都没有任何显示。您能否查看我现在添加的完整文件集,并查看是否有什么原因使您的解决方案在我的端上无法工作? - Calaf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接