ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 以百分比设置按钮的大小

2019-08-25 13:32:12  阅读:149  来源: 互联网

标签:android java user-interface eclipse android-button


请查看以下代码

<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>

这正在我的设备上工作.希望这也适合你!

标签:android,java,user-interface,eclipse,android-button
来源: https://codeday.me/bug/20190825/1718945.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有