我有这些单选按钮,他们需要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:layout_wIDth替换androID:wIDth但是他们没有显示.
我也尝试过使用dip,但是按钮没有显示出来.
我还应该补充一点,我使用的是drawables而不是stock单选按钮.我不知道这是否有所作为,但是drawables的大小都相同(50px x 50px).
有谁知道我如何设置这些的高度和宽度,以便他们将自己调整到不同的屏幕尺寸?
解决方法:
使用dp而不是px设置高度和宽度.有关这些单元的更多信息,请参阅此帖子的答案. What is the difference between “px”, “dp”, “dip” and “sp” on Android?
我还建议你在支持多个屏幕时熟悉androID的documentation.
好的,所以我花了一些时间来制作一个针对AndroID 1.5的快速示例.
你可以找到source here.
简而言之,您需要执行以下步骤:
将图像放在res / drawable中.你应该至少有4个图标:pressed&未经检查,按下&检查,未按下&未经检查,未按下&经过
在res / drawable中创建选择器类型布局.这是我的:
<?xml version="1.0" enCoding="utf-8"?><selector xmlns:androID="http://schemas.androID.com/apk/res/androID"> <item androID:state_checked="true" androID:state_pressed="false" androID:drawable="@drawable/radio_on"/> <item androID:state_checked="false" androID:state_pressed="false" androID:drawable="@drawable/radio_off"/> <item androID:state_checked="true" androID:state_pressed="true" androID:drawable="@drawable/radio_on_pressed"/> <item androID:state_checked="false" androID:state_pressed="true" androID:drawable="@drawable/radio_off_pressed"/></selector>
然后像这样设置你的RadioGroup:
<RadioGroup androID:layout_wIDth="fill_parent" androID:layout_height="50dp" androID:orIEntation="horizontal" androID:checkedbutton="@+ID/first"> <Radiobutton androID:ID="@+ID/first" androID:wIDth="50dp" androID:height="50dp" androID:button="@drawable/button_radio"/> <Radiobutton androID:ID="@+ID/second" androID:wIDth="50dp" androID:height="50dp" androID:button="@drawable/button_radio"/> <Radiobutton androID:ID="@+ID/third" androID:wIDth="50dp" androID:height="50dp" androID:button="@drawable/button_radio"/> <Radiobutton androID:ID="@+ID/fourth" androID:wIDth="50dp" androID:height="50dp" androID:button="@drawable/button_radio"/></RadioGroup>
我指定了50dp的尺寸,因为我的drawable的尺寸是50px x 50px.另请注意我设置androID:按钮而不是androID:background.
这是上面项目的签名APK:Custom RadioButton(注意:它的目标是SDK版本4,所以它不会请求SD和电话权限)
这应该给出以下结果:
希望这可以帮助.
总结以上是内存溢出为你收集整理的如何根据屏幕尺寸设置单选按钮的宽度? (机器人)全部内容,希望文章能够帮你解决如何根据屏幕尺寸设置单选按钮的宽度? (机器人)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)