2009-02-10 80 views
4

我已经使用XML布局创建器在LinearLayout(下面的XML中包含的XML)中使用RadioGroup在Android中创建了自定义图标栏。 RadioGroup中的每个RadioButton都有一个自定义可绘制(带有选中和未选中状态)作为其android:button属性。如何使用XML根据图像大小调整Android组件的大小

线性布局本身在RelativeLayout之内,因此它可以出现在屏幕上的任意位置(在本例中为侧栏)。

绘图宽55像素,所有这些视图的android:layout\_width属性是wrap\_content

当我使这个组件可见时,与屏幕的左下角对齐,只有大约四分之三的RadioButton图像宽度可见。将RelativeLayoutlayout\_width设置为fill\_parent可解决此问题并导致出现完整按钮。

但是这意味着按钮组消耗整个屏幕宽度的点击事件。

如何让整个按钮显示,并且只有按钮的区域响应点击?对drawable的宽度进行硬编码并不是特别理想的解决方案。

<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
    <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_below="@+id/LinearLayout01" android:visibility="invisible"> 
     <RadioGroup android:id="@+id/RadioGroup01" android:layout_height="wrap_content" android:checkedButton="@+id/RB01" android:layout_width="fill_parent"> 
      <RadioButton android:id="@+id/RB01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:button="@drawable/DR01"/> 
      <RadioButton android:id="@+id/RB02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:button="@drawable/DR02"/> 
      <RadioButton android:id="@+id/RB03" android:layout_width="fill_parent" android:layout_height="wrap_content" android:button="@drawable/DR03"/> 
      <RadioButton android:id="@+id/RB04" android:layout_width="fill_parent" android:layout_height="wrap_content" android:button="@drawable/DR04"/> 
     </RadioGroup> 
    </LinearLayout> 
    </RelativeLayout> 

回答

7

设置的LinearLayout,RadioGroup中,或单选按钮的DP的(密度indpendent像素)的宽度可能为你工作。根据你上面所说的,我认为它必须是布局的宽度。尺寸是“硬编码”的,但它们会随着屏幕的大小而扩展。

因此XML可能看起来像:

android:layout_width="55dp" 

对DPS的官方文档是here

+2

为什么55 dp?所有Android的宽度为220dp? – Jaseem 2011-12-30 11:48:28

1

我无法想象为什么其他的答案有6个upvotes,这是完全错误的

这些尺寸是“硬编码”的,但它们会随着屏幕的大小而变化。

没有他们将规模根据你的屏幕密度。我非常希望我们可以使用XML中的百分比,而不必编写它们。

+0

'因为它工作;) - 一个可见的变化! – 2013-01-03 16:27:24

相关问题