Android TableLayout宽度问题

发布于 2024-10-10 11:17:32 字数 1074 浏览 0 评论 0原文

我正在使用 TableLayout 来显示数据。 当 Activity 调用 onCreate() 时,将设置右列 TextView 的文本。

现在,如下图所示,我的地址文本可能很长,应该换行。 所以我设置了android:layout_width="wrap_content"。但仍然需要屏幕大小的宽度来包装数据。 我该如何克服这个问题?

alt text

我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5px">
    <TableRow>
        <TextView android:text="Job#"
            android:paddingRight="5px" />
        <TextView android:id="@+id/DetailJobNo" />
    </TableRow>
    <TableRow>
        <TextView android:text="Address"
            android:paddingRight="5px" />
        <TextView android:id="@+id/DetailAddress"
            android:layout_width="wrap_content"
            android:text="Address Address Address Address Address Address Address Address "/>
    </TableRow>
</TableLayout>

I'm using TableLayout to display data.
Text of the TextViews of the right column will be set when activity calls onCreate().

Now, as you can see in the following image that my address text can be long and it should be wrapped.
So I set android:layout_width="wrap_content". but it still take a width of screen size to wrap data.
How can I overcome from this issue?

alt text

My xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5px">
    <TableRow>
        <TextView android:text="Job#"
            android:paddingRight="5px" />
        <TextView android:id="@+id/DetailJobNo" />
    </TableRow>
    <TableRow>
        <TextView android:text="Address"
            android:paddingRight="5px" />
        <TextView android:id="@+id/DetailAddress"
            android:layout_width="wrap_content"
            android:text="Address Address Address Address Address Address Address Address "/>
    </TableRow>
</TableLayout>

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

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

发布评论

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

评论(3

赠佳期 2024-10-17 11:17:32

android:shrinkColumns="1" 添加到 TableLayout 解决了我的问题。

Adding android:shrinkColumns="1" to TableLayout solves my issue.

莫多说 2024-10-17 11:17:32

您可以尝试将 setHorizo​​ntallyScrolling 设置为 DetailAdress TextView 为 false?

Can you try setting setHorizontallyScrolling to the DetailAdress TextView to false?

多情出卖 2024-10-17 11:17:32

@Vikas,很高兴你解决了你的问题。

但出于安全目的,我仍然建议使用 Horizo​​ntalScrollView。如果有几个字符不可见,那么水平滚动条将有助于管理它。 XML 文本:

<HorizontalScrollView android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp" android:layout_marginBottom="10dp" >

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"   
android:layout_width="fill_parent"   
android:layout_height="fill_parent"   
android:padding="5px">   
  ......
  ......
  ADD YOUR ROWS 
 </TableLayout>       
</HorizontalScrollView>

处理意外情况总是更好。

@Vikas, its good that you have solved your problem.

BUt I would still recommend to use HorizontalScrollView for security purposes. If at all few chars are not visible, then horizontal scrollbar will help to manage that. XML text :

<HorizontalScrollView android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp" android:layout_marginBottom="10dp" >

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"   
android:layout_width="fill_parent"   
android:layout_height="fill_parent"   
android:padding="5px">   
  ......
  ......
  ADD YOUR ROWS 
 </TableLayout>       
</HorizontalScrollView>

Its always better to take care of the unexpected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文