2010-12-01 115 views
24

尽管文字包装为inside the cell,但无法让我的TableLayout适合屏幕when the text cell is largeAndroid桌面布局格式问题

这是我的TableLayout。一个简单的两行,两列表格。左列单元格是multiline。如果其中一个单元格的文本足够大,可以分成两行,则该列将被拉伸以填充整个屏幕,并且右列将被推出屏幕。

为什么?我如何强制整个桌子留在屏幕内? 欢迎任何提示。

我TableLayout如下:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/myTableLayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<TableRow> 
    <TextView 
     android:text="Account 4 with a very large name to see if it breaks on two or more lines" 
     android:singleLine="false" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     > 
    </TextView> 
    <TextView 
     android:text="$400.00" 
     android:gravity="right" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     > 
    </TextView> 
</TableRow> 

<TableRow> 
    <TextView 
     android:text="Account 5" 
     android:singleLine="false" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     > 
    </TextView> 
    <TextView 
     android:text="$400.00" 
     android:gravity="right" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     > 
    </TextView> 
</TableRow> 

</TableLayout> 

的代码显示布局是基本的:

public class AccountBrowserTest 
    extends Activity 
{ 
    public void onCreate(Bundle savedInstanceState) 
    {  
     super.onCreate(savedInstanceState); 

     this.setContentView(R.layout.accountbrowser_test); 

    } 
} 

回答

32

添加机器人:stretchColumnsandroid:shrinkColumnsTableLayout元素:

<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="1" 
    android:shrinkColumns="1"> 

1值要发生的变化列。您可以指定一个以上的价值是这样的:

android:stretchColumns="0,1" 

,或者如果你想改变发生在你列如下:

android:stretchColumns="*" 

请看看这里的详细信息:http://android-pro.blogspot.com/2010/02/table-layout.html

0

它可能有与wrap_content你多单元格的宽度做。

你可以尝试一些方法给它一个更明确的宽度...即设置width to 0,并给所有的单元格layout_gravity = 1。这应该会导致每列都占用相等数量的宽度(或者您可以调整,如果您希望一个比另一个更重)。

0

免责声明:我不是布局真的好,所以我只周围的一些价值观位出场,这似乎工作(我不能给你一个很好的解释,为什么虽然):

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myTableLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TableRow> 
     <TextView 
      android:text="Account 4 with a very large name to see if it breaks on two or more lines" 
      android:singleLine="false" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
     /> 
     <TextView 
      android:text="$400.00" 
      android:gravity="right" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.001" 
     /> 
    </TableRow> 

    <TableRow> 
     <TextView 
      android:text="Account 5" 
      android:singleLine="false" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
     /> 
     <TextView 
      android:text="$400.00" 
      android:gravity="right" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.001" 
     /> 
    </TableRow> 
</TableLayout> 

另外请注意,我没有在Activity中尝试过这种布局,我只是在xml编辑器的布局视图中查看它,但至少在那里看起来很好。

0

您也可以尝试在TableLayout中操作stretchColumns和/或shrinkColumns。

8

我有同样的问题,我的应用程序,发现如下解决方案:

<TableLayout 
    android:shrinkColumns="0" 
    android:stretchColumns="1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
</TableLayout> 

能正常工作2列。

5

我有同样的问题,并通过添加

android:stretchColumns="0" 

到TableLayout元素和过长的TextView的设置

android:layout_width="0dp" 

解决它。

+2

以及究竟这意味着什么?你怎么知道这是解决方案? – Paul 2012-02-08 14:11:28

1

有临时的解决办法,这样做:

android:stretchColumns = " (number of actual col) - 1 " 

这里没有解决我的情况下工作。

5

里面的表格布局使用的shrinkColumns属性列你想让它们缩小以便装配

最简单的方法是:

<TableLayout 
android:shrinkColumns="*"/> 

缩小所有列,或者你可以写,而不是“*”,你要收缩的列索引,而你的情况将是

<TableLayout 
android:shrinkColumns="0,1"/> 

注意,列索引从0开始,所以最新的代码允许第一个(索引= 0)和第二个(索引= 1)列缩小。

0

希望这个代码将会帮助你.. 公共类MainActivity扩展活动{

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

<TextView 
     android:id="@+id/tv1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="8dp" 
     android:singleLine="false" 
     android:text="Account 4 with a very large \n name to see if it breaks \n on two or more lines" > 
    </TextView> 

    <TextView 
     android:id="@+id/tv2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="8dp" 
     android:text="$400.00" > 
    </TextView> 
</TableRow>