首页 / JAVA / java – 以百分比设置按钮的大小
java – 以百分比设置按钮的大小
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 以百分比设置按钮的大小,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5708字,纯文字阅读大概需要9分钟。
内容图文
![java – 以百分比设置按钮的大小](/upload/InfoBanner/zyjiaocheng/732/44fb2bfbf82b400fbcf525567ecb9847.jpg)
请查看以下代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HomeScreen" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"
android:layout_alignParentTop="true" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
</TableLayout>
</RelativeLayout>
这会生成以下输出.
但这不是我需要的.我需要这12个按钮来填充整个屏幕,彼此之间保持相等的空间并且尺寸也相等.我不需要设置固定大小谎言高度= 100,宽度= 100,而是应该根据设备屏幕大小调整大小.这意味着,每个按钮在屏幕上可以有8.3%的空间(100/12 = 8.3).
我通过将每个按钮的高度设置为0dp并添加android:layout_weight = 0.083来尝试此操作.但这不起作用,因为此后没有显示按钮.
我怎样才能做到这一点?
解决方法:
尝试以下布局.我已经更改了一些权重值并将按钮高度设置为fill_parent
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HomeScreen" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"
android:weightSum="4" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Button" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
<Button
android:id="@+id/button11"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
<Button
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
</TableRow>
</TableLayout>
</RelativeLayout>
这正在我的设备上工作.希望这也适合你!
内容总结
以上是互联网集市为您收集整理的java – 以百分比设置按钮的大小全部内容,希望文章能够帮你解决java – 以百分比设置按钮的大小所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。