如何使用 XML 根据图像大小调整 Android 组件的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/531819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to size Android components according to image size using XML
提问by funkybro
I have created a custom icon bar in Android using the XML layout creator, using a RadioGroup within a LinearLayout (XML included below). Each RadioButton within the RadioGroup has a custom drawable (with checked and unchecked states) as its android:button
attribute.
我使用 XML 布局创建器在 Android 中创建了一个自定义图标栏,使用 LinearLayout 中的 RadioGroup(下面包含 XML)。RadioGroup 中的每个 RadioButton 都有一个自定义 drawable(具有选中和未选中状态)作为其android:button
属性。
The Linear layout is itself within a RelativeLayout
so that it can appear at an arbitrary position on the screen (as a side bar, in this instance).
线性布局本身在 a 中,RelativeLayout
因此它可以出现在屏幕上的任意位置(在这种情况下作为侧栏)。
The drawables are 55 pixels wide, and the android:layout\_width
attribute of all these views is wrap\_content
.
可绘制对象的宽度为 55 像素,android:layout\_width
所有这些视图的属性都是wrap\_content
.
When I make this component visible, aligned to the bottom-left of the screen, only about three-quarters of the RadioButton image widths are visible. Setting the layout\_width
of the RelativeLayout
to fill\_parent
rectifies this and causes the full button to appear.
当我使这个组件可见并与屏幕左下角对齐时,只有大约四分之三的 RadioButton 图像宽度可见。设置layout\_width
的RelativeLayout
,以fill\_parent
改正了这并使全按钮出现。
However this means that the button group consumes click events across the entire width of the screen.
然而,这意味着按钮组在整个屏幕宽度上消耗点击事件。
How can I make the entire button appear, and have only the area of the button respond to clicks? Hard-coding the width of the drawable is not a particularly desirable solution.
如何让整个按钮出现,并且只有按钮区域响应点击?硬编码 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>
回答by Will
Setting the width of the LinearLayout, RadioGroup, or RadioButton in dp's (density indpendent pixels) might work for you. Based on what youve said above I think it would have to be the Layout's width. The dimensions are "hardcoded" but they'll scale with the size of your screen.
在 dp(密度独立像素)中设置 LinearLayout、RadioGroup 或 RadioButton 的宽度可能对您有用。根据你上面所说的,我认为它必须是布局的宽度。尺寸是“硬编码”的,但它们会随着屏幕的大小而缩放。
So the xml could look like:
所以 xml 可能看起来像:
android:layout_width="55dp"
The official documentation for dps is here
dps的官方文档在这里
回答by pjcard
I can't imagine why the other answer has 6 upvotes, it is entirely wrong
我无法想象为什么另一个答案有 6 个赞成票,这是完全错误的
The dimensions are "hardcoded" but they'll scale with the size of your screen.
尺寸是“硬编码”的,但它们会随着屏幕的大小而缩放。
Nothey will scale based on the densityof your screen. I dearly wish we could use percentages from XML, rather than having to code them.
不,它们会根据您的屏幕密度进行缩放。我非常希望我们可以使用 XML 中的百分比,而不必对它们进行编码。