2017-06-03 202 views
0

我已经尽了我的努力寻找网络,但我似乎无法为以下找到适当的解决方案。Android按钮对齐

我正在使用表格布局和表格行,并且我正在使用TEXT的3个按钮。以下是布局文件的示例。

<?xml version="1.0" encoding="utf-8"?> 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tlRemoteTable" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:stretchColumns="*"> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="ABCD" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="ABCDEFGHI" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="ABCDEFGHIJKLMNOP" /> 
    </TableRow> 

</TableLayout> 

图像的结果是在这里:

Uneven Layout

我的目标是使之更,所有有1/3的比例。我知道文本长度是迫使它扩展的文本长度。我如何包装文字呢?

最终结果将是一个动态生成的文件。所以我会编写它,而不是XML。

回答

0
<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tlRemoteTable" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:stretchColumns="*"> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1.0f"> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:text="ABCD" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:text="ABCDEFGHI" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:text="ABCDEFGHIJKLMNOP" /> 
    </TableRow> 

</TableLayout> 

您可以更改按钮宽度0dp所有组的工作

0

谢谢!这工作!

下面是程序代码,如果有人需要它!

TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT, 1.0f); 
TableRow.LayoutParams btnParams = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT);