【问题标题】:Setting the size of buttons in percentage以百分比设置按钮的大小
【发布时间】:2013-10-18 11:58:46
【问题描述】:

请看下面的代码

<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。但这不起作用,因为在此之后没有显示任何按钮。

我该怎么做?

【问题讨论】:

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


    【解决方案1】:

    你不能在 xml 中拥有你想要的动态布局..

    在您加载布局的活动/片段中......您必须根据屏幕尺寸自行设置 tableRow 高度

    要获得屏幕尺寸,您可以使用

        DisplayMetrics metrics = new DisplayMetrics();
        Activity aActivity = (Activity)  container.getContext();
        aActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
        screen_Width = metrics.widthPixels;
        screen_Height = metrics.heightPixels;
    

    现在您可以将每个表格的行宽设置为totalHeight/NoOFRows - padding.

            tableRow.setMinimumHeight(calculatedHeight);
    

    可以对宽度进行类似的计算,以使它们正确填充并保持相同的大小

    【讨论】:

    • 非常感谢您的回复。以后肯定会用。谢谢!
    【解决方案2】:

    尝试以下布局。我更改了一些权重值并将按钮高度设置为 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>
    

    这在我的设备上运行。希望这也适用于你!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      相关资源
      最近更新 更多