Android表格布局格式化问题

发布于 2024-10-05 15:59:49 字数 1691 浏览 4 评论 0原文

当文本单元格很大时,我无法让 TableLayout 适合屏幕,尽管文本在单元格内换行。

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

为什么?如何强制整个表格保留在屏幕内? 欢迎任何提示。

我的 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);

    }
} 

I can not get my TableLayout to fit inside the screen when the text cell is large, despite the text wraps inside the cell.

This is my TableLayout. A simple two-rows, two-columns table. Left column cells are multiline. If the text of one of these cell is large enought to break in two lines, the column is stretched to fill the entire screen and the right columns is pushed out of the screen.

Why? How can I force the full table to stay inside the screen?
Any hint is welcomed.

My TableLayout is the following:

<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>

The code to display the layout is basic:

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

        this.setContentView(R.layout.accountbrowser_test);

    }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

漫漫岁月 2024-10-12 15:59:50

将 android:stretchColumnsandroid:shrinkColumns 添加到 TableLayout 元素:

<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

Add android:stretchColumns and android:shrinkColumns to the TableLayout element:

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

The 1 values are the columns you want the changes to occur. You can specify more that one value like this:

android:stretchColumns="0,1"

or if you want the changes to happen to all you columns like this:

android:stretchColumns="*"

Please look here for detailed information: http://android-pro.blogspot.com/2010/02/table-layout.html

习ぎ惯性依靠 2024-10-12 15:59:50

我的应用程序遇到了同样的问题,并找到了以下解决方案:

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

这适用于 2 列。

i had the same problem with my app and found the following solution:

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

This works fine with 2 Columns.

半﹌身腐败 2024-10-12 15:59:50

我遇到了同样的问题,并通过添加

android:stretchColumns="0"

到 TableLayout 元素并设置

android:layout_width="0dp"

太长的 TextView 来解决它。

I had the same problem and solved it by adding

android:stretchColumns="0"

to the TableLayout element and setting

android:layout_width="0dp"

on the TextView that was too long.

生死何惧 2024-10-12 15:59:50

在表格布局内,对您希望允许收缩的列使用shrinkColumns属性,

最简单的方法是:

<TableLayout
android:shrinkColumns="*"/>

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

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

请注意,列索引从零开始,因此最新代码允许第一列(索引=0)和第二列(索引=1)收缩。

inside table layout use shrinkColumns property for columns you want them to be allowed to shrink for fitting

easiest way is:

<TableLayout
android:shrinkColumns="*"/>

to shrink all columns or you can write instead of " *" ,the column indices you want to shrink , which in your case will be

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

note that column indices start from zero , so the latest code allows first (index=0) and second (index =1) columns to shrink.

与他有关 2024-10-12 15:59:50

有一个临时修复,请执行:

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

这里没有解决方案适用于我的情况。

There is temporary fix, do:

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

No solution here worked in my case.

初见终念 2024-10-12 15:59:50

免责声明:我不太擅长布局,所以我只尝试了一些值,这似乎有效(但我无法给你一个很好的解释):

<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 编辑器的布局视图中查看它,但至少在那里看起来不错。

Disclaimer: I'm not really good with layouts, so I only played around a bit with some values, and this seemed to work (I cannot give you a good explanation of why though):

<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>

Also note that I haven't tried this layout in an Activity, I only looked at it in the layout-view of the xml editor, but it looked fine there at least.

不必了 2024-10-12 15:59:50

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

You could also try manipulating stretchColumns and/or shrinkColumns in the TableLayout.

老娘不死你永远是小三 2024-10-12 15:59:50

这可能与多行单元格宽度的 wrap_content 有关。

您可以尝试几种方法来赋予它更明确的宽度...即将宽度设置为0,并为所有单元格赋予layout_gravity = 1。这应该会导致每一列占据相同的宽度(或者如果您希望一列比另一列具有更大的权重,则可以进行调整)。

It might have to do with the wrap_content for the width of your multiline cells.

You could try a few ways of giving it a more explicit width... i.e., setting the width to 0, and giving all of the cells a layout_gravity = 1. That should cause each of the columns to take up an equal amount of width (or you can adjust if you want one to have more weight than the other).

北恋 2024-10-12 15:59:50

希望这段代码能帮助你..
公共类 MainActivity 扩展 Activity {

@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>

Hope this code will help you..
public class MainActivity extends Activity {

@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>
冧九 2024-10-12 15:59:50

您需要很好地处理布局权重的组合。检查下面的示例。使用layout_weight = 1和layout_width = 0dp,行中的所有视图应获得相同的宽度。

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*"
android:stretchColumns="*">
<TableRow
    android:id="@+id/basicKeypadRow1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
    <TextView
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="T">
    </TextView>
    <TextView
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="R">
    </TextView>
    <TextView
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Y">
    </TextView>
</TableRow>
<TableRow
        android:id="@+id/basicKeypadRow1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <TextView
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="R">
        </TextView>
        <TextView
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="O">
        </TextView>
        <TextView
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="W">
        </TextView>
    </TableRow>

You need to have a combination of layout weights handled nicely. Check the example below. All the views in the row should get the same width using layout_weight=1 and layout_width=0dp.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*"
android:stretchColumns="*">
<TableRow
    android:id="@+id/basicKeypadRow1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
    <TextView
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="T">
    </TextView>
    <TextView
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="R">
    </TextView>
    <TextView
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Y">
    </TextView>
</TableRow>
<TableRow
        android:id="@+id/basicKeypadRow1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <TextView
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="R">
        </TextView>
        <TextView
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="O">
        </TextView>
        <TextView
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="W">
        </TextView>
    </TableRow>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文