2017-06-03 204 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);