Android:按钮离开屏幕边缘

时间:2015-07-12 20:51:28

标签: android button tablerow

我在线性布局中有一个表格布局 在表格行中按下3个按钮后,最后一个按钮离开屏幕 我的意思是3个按钮不适合屏幕宽度 我该怎么办?

这是代码

<TableLayout
    android:id="@+id/buttonTableLayout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:stretchColumns="0,1,2"
    android:layout_weight="1">

    <TableRow
        android:id="@+id/tableRow0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TableRow>

</TableLayout>

1 个答案:

答案 0 :(得分:0)

TableRow不会检查其内容是否适合屏幕尺寸。我猜你的按钮占据了所有空间,因此最后的按钮位于屏幕之外。

我可以想到这个问题的2个解决方案:

  • 对每个按钮使用权重为LinearLayout:

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:layout_weight="1.0" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:layout_weight="1.0" />
    
    </LinearLayout>
    

    当然,您也可以通过编程方式执行此操作。 有关权重的更多信息,请访问:https://developer.android.com/guide/topics/ui/layout/linear.html

  • 使用Flow-Layout。它会自动检查您的视图是否适合当前行,如果没有足够的空间,则将它们放在下一行中。我自己使用的一个好的开源库就是这个:https://github.com/ApmeM/android-flowlayout

相关问题