如何在样式文件中定义drawable的大小?

时间:2013-05-22 16:35:52

标签: android android-layout android-view dry android-drawable

我想在 res / values / styles.xml 中只指定一次两个复选框及其四个形状drawable的大小

  1. RES /抽拉/ cb1_checked.xml
  2. RES /抽拉/ cb1_unchecked.xml
  3. RES /抽拉/ cb2_checked.xml
  4. RES /抽拉/ cb2_unchecked.xml 即可。
  5. 这样,大小会在样式中出现一次,而不是在drawable中出现四次。

    以下两项尝试都不起作用。你能建议一个解决方案吗? (如果您发现这两次尝试有什么问题,请提及。)

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

    替换

    RES /抽拉/ cb1_checked.xml

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

    RES /值/ styles.xml

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

    ----------------------------------------------- ----------------- 尝试2 ------------------------ ----------------------------------------

    添加

    RES /值/ styles.xml

    <item name="android:background">cb_background</item>
    

    并定义背景可绘制

    RES /抽拉/ 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 /布局/ 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 /值/ 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 /抽拉/ 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 /抽拉/ 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 /抽拉/ 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选择器和已检查/未选中的drawables)

2 个答案:

答案 0 :(得分:6)

有一个弱解决方案,弱 android:width android:height 仍会出现四次,但至少可以通过设置调整尺寸只有一个参数。

对于四个drawable中的每一个,添加

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

并定义维度

<强> RES /值/ dimens.xml

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

所以问题仍然存在:可以在drawables中根本没有指定大小吗?

答案 1 :(得分: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" />