如何使用XML根据图像大小调整Android组件的大小

fun*_*bro 4 xml layout android

我使用XML布局创建器在Android中创建了一个自定义图标栏,使用LinearLayout中的RadioGroup(下面包含XML).RadioGroup中的每个RadioButton都有一个自定义drawable(带有checked和unchecked状态)作为其android:button属性.

线性布局本身在a内,RelativeLayout因此它可以出现在屏幕上的任意位置(在这种情况下为侧边栏).

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

当我使该组件可见时,对齐到屏幕的左下角,只有大约四分之三的RadioButton图像宽度可见.设置layout\_widthRelativeLayout,以fill\_parent 整流这一点,并导致全按钮出现.

但是,这意味着按钮组会在整个屏幕宽度上消耗点击事件.

如何显示整个按钮,并且只有按钮区域响应点击?硬编码可拉伸的宽度不是特别理想的解决方案.

<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>
Run Code Online (Sandbox Code Playgroud)

Wil*_*ill 7

在dp(密度独立像素)中设置LinearLayout,RadioGroup或RadioButton的宽度可能对您有用.根据你上面所说的,我认为它必须是布局的宽度.尺寸是"硬编码的",但它们会根据屏幕尺寸进行缩放.

所以xml看起来像:

android:layout_width="55dp"
Run Code Online (Sandbox Code Playgroud)

dps的官方文档在这里

  • 为什么55 dp?所有android都有220dp的宽度? (2认同)