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

前端之家收集整理的这篇文章主要介绍了android – 如何在样式文件中定义drawable的大小?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在res / values / styles.xml中只指定一次两个复选框及其四个形状drawable的大小

> res / drawable / cb1_checked.xml
> res / drawable / cb1_unchecked.xml
> res / drawable / cb2_checked.xml
> res / drawable / cb2_unchecked.xml.

这样,尺寸将在样式中出现一次,而不是在drawable中出现四次.

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

————————————————– ————–尝试1 ———————————- ——————————

更换

RES /抽拉/ cb1_checked.xml

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

RES /值/ styles.xml

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

————————————————– ————–尝试2 ———————————- ——————————

RES /值/ styles.xml

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

并定义一个可绘制的背景

RES /抽拉/ cb_background.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle">
  4. <solid android:color="#00FF00"/>
  5. <size android:width="24dp"
  6. android:height="24dp" />
  7. </shape>

————————————————– – – – – – – – 添加 – – – – – – – – – – – – – – – – – – —————————–

以下是完整的XML文件集:

RES /布局/ activity_main.xml中

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/MyLL"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content">
  6.  
  7. <CheckBox
  8. android:id="@+id/cb1"
  9. style="@style/CB_style"
  10. android:button="@drawable/cb1_selector"
  11. android:height="@dimen/my24sp" />
  12.  
  13. <CheckBox
  14. android:id="@+id/cb2"
  15. style="@style/CB_style"
  16. android:button="@drawable/cb2_selector"
  17. android:height="@dimen/my24sp" />
  18.  
  19. </LinearLayout>

RES /值/ styles.xml

  1. <style name="AppBaseTheme" parent="android:Theme.Light">
  2. </style>
  3.  
  4. <style name="AppTheme" parent="AppBaseTheme">
  5. </style>
  6.  
  7. <style name="CB_style" parent="@android:style/TextAppearance.Medium">
  8. <item name="android:layout_width">wrap_content</item>
  9. <item name="android:layout_height">wrap_content</item>
  10. <item name="android:layout_weight">1</item>
  11. <item name="android:layout_gravity">center_horizontal</item>
  12. <item name="android:gravity">center</item>
  13. <item name="android:checked">false</item>
  14. </style>

RES /抽拉/ cb1_selector.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:constantSize="true">
  4.  
  5. <item android:state_checked="true"
  6. android:drawable="@drawable/cb1_checked" />
  7.  
  8. <item android:state_checked="false"
  9. android:drawable="@drawable/cb1_unchecked" />
  10.  
  11. </selector>

RES /抽拉/ cb1_checked.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle">
  4. <solid android:color="#FF0000"/>
  5. <size android:width="24dp"
  6. android:height="24dp" />
  7. </shape>

RES /抽拉/ cb1_unchecked.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle">
  4. <solid android:color="#880000"/>
  5. <size android:width="24dp"
  6. android:height="24dp" />
  7. </shape>

(类似于cb2选择器和已检查/未选中的drawables)

解决方法

有一个弱解决方案,在android:width和android:height仍然出现四次的意义上很弱,但至少可以通过设置一个参数来调整维度.

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

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

并定义维度

RES /值/ dimens.xml

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

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

猜你在找的Android相关文章