如何设置单选按钮的宽度以根据屏幕尺寸进行更改? (安卓)
我有这些单选按钮,它们需要 android:width="X"
和 android:height"X"
但是,我不知道如何设置这些属性,以便它们适应不同的屏幕尺寸。
这是我用于按钮的代码:
<RadioGroup android:layout_width="fill_parent"
android:layout_height="50px" android:orientation="horizontal"
android:checkedButton="@+id/first" android:id="@+id/states">
<RadioButton android:id="@+id/first"
android:background="@drawable/button_radio" android:width="50px"
android:height="50px" />
<RadioButton android:id="@+id/second"
android:background="@drawable/button_radio" android:width="50px"
android:height="50px" />
<RadioButton android:id="@+id/third"
android:background="@drawable/button_radio" android:width="50px"
android:height="50px" />
<RadioButton android:id="@+id/fourth"
android:background="@drawable/button_radio" android:width="50px"
android:height="50px" />
<RadioButton android:id="@+id/fifth"
android:background="@drawable/button_radio" android:width="50px"
android:height="50px" />
</RadioGroup>
我尝试将单选按钮放在表格行中,但它们无法正常运行。当我单击一个时,它会选择它,但当我单击另一个时,它不会取消选择它。
我尝试将 android:width
替换为 android:layout_width
但随后它们不显示。
我也尝试过使用dip,但按钮没有显示。
我还应该补充一点,我使用的是可绘制对象而不是库存单选按钮。我不知道这是否会产生影响,但可绘制的尺寸都是相同的(50px x 50px)。
有谁知道我如何设置它们的高度和宽度,以便它们能够根据不同的屏幕尺寸进行调整?
I have these radio buttons and they need android:width="X"
and android:height"X"
however, I do not know how to set these properties so that they adapt to a different screen size.
Here is the code I'm using for my buttons:
<RadioGroup android:layout_width="fill_parent"
android:layout_height="50px" android:orientation="horizontal"
android:checkedButton="@+id/first" android:id="@+id/states">
<RadioButton android:id="@+id/first"
android:background="@drawable/button_radio" android:width="50px"
android:height="50px" />
<RadioButton android:id="@+id/second"
android:background="@drawable/button_radio" android:width="50px"
android:height="50px" />
<RadioButton android:id="@+id/third"
android:background="@drawable/button_radio" android:width="50px"
android:height="50px" />
<RadioButton android:id="@+id/fourth"
android:background="@drawable/button_radio" android:width="50px"
android:height="50px" />
<RadioButton android:id="@+id/fifth"
android:background="@drawable/button_radio" android:width="50px"
android:height="50px" />
</RadioGroup>
I have tried putting the Radio buttons in a table row, but then they don't operate properly. When I click one, it selected it, but didn't unselect it when I clicked another.
I have tried replacing the android:width
with android:layout_width
but then they don't show.
I have also tried using dip, but then the buttons didn't show up.
I should also add that I am using drawables instead of the stock radio buttons. I don't know if this would make a difference, but the drawables are all the same size (50px x 50px).
Does anyone know how I can set the height and width of these so they will adjust themselves to different screen sizes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 dp 而不是 px 设置高度和宽度。有关单位的更多信息,请参阅这篇文章的答案。 “px”、“dp”、“之间有什么区别Android 上的“dip”和“sp”?
我还建议您熟悉 android 的 有关支持多屏幕的文档。
好的,我花了一些时间快速编写了一个针对 Android 1.5 的示例。
您可以此处找到源。
简而言之,您需要执行以下步骤:
将图像放入
res/drawable
中。您应该至少有 4 个图标:按下和按下。未选中、已按下和已选中选中,未按下&未选中、未按下 &选中在 res/drawable 中创建一个
selector
类型布局。这是我的:然后像这样设置您的
RadioGroup
:我已指定尺寸为 50dp,因为我的可绘制对象的尺寸为 50px x 50px。另请注意,我设置的是
android:button
而不是android:background
。以下是上述项目的签名 APK: Custom RadioButton (注意:它是针对 SDK版本 4,因此它不会请求 SD 和电话权限)
这应该给出以下结果:
希望这会有所帮助。
Set the height and width using dp instead of px. See the answer to this post for more information on the units. What is the difference between "px", "dp", "dip" and "sp" on Android?
I would also suggest you familiarize yourself with android's documentation on supporting multiple screens.
Okay, so I've taken a moment to whip up a quick example targeted at Android 1.5.
You can find the source here.
In short, you need to take the following steps:
Place your images in
res/drawable
. You should have at least 4 icons: Pressed & Unchecked, Pressed & Checked, Not Pressed & Unchecked, Not Pressed & CheckedCreate a
selector
type layout in res/drawable. Here is mine:Then set-up your
RadioGroup
like so:I have specified dimension of 50dp as the dimension of my drawables are 50px x 50px. Also notice I am setting
android:button
and notandroid:background
.Here is a signed APK of the project above: Custom RadioButton (Note: it is targeted to SDK version 4 so it wouldn't request SD and Phone permissions)
This should give the following result:
Hope this helps.
我建议您使用 radioGroup 尝试以下操作:
这样,所有单选按钮将占据相同的空间。此外,它会根据屏幕尺寸而变化。
I would suggest you to try the following with your radioGroup :
This way all of your radio buttons will occupy equal space. Moreover, it will change as per the screen size.