【问题标题】:Android question about TableLayout and ellipsis关于 TableLayout 和省略号的 Android 问题
【发布时间】:2016-08-25 11:56:19
【问题描述】:

我的 TableLayout 有问题。它由两列和多行组成。当第二列的 TextView 包含较长宽度的文本时,它将将该列(在下面的行中)中的 TextView 推离屏幕(向右)。相反,我希望它将文本保留在左侧,并在末尾用省略号覆盖文本。谁能看到我的 XML 有什么问题?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4096cc" >

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/roundedshape"
        android:layout_margin="5dp"
        android:stretchColumns="1">

        <TableRow>
            <TextView
                android:layout_column="1"
                android:text="Server Name"
                android:textStyle="bold"
                android:padding="10dip" />
            <TextView
                android:id="@+id/txtServerName"
                android:text=""
                android:gravity="right"
                android:ellipsize="end"
                android:padding="10dip" />
        </TableRow>

        <View
            android:layout_height="2dip"
            android:background="#FF909090" />

        <TableRow>
            <TextView
                android:layout_column="1"
                android:text="Status"
                android:textStyle="bold"
                android:padding="10dip" />
            <ImageView
                android:id="@+id/status"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:padding="10dip"
                android:src="@drawable/icon" />
        </TableRow>
            .
            .
            .

    </TableLayout>
</LinearLayout>

【问题讨论】:

    标签: android xml layout tablelayout


    【解决方案1】:

    根据你想在哪一列放上限,你需要使用

    android:shrinkColumns=x 
    

    在您的TableLayout 中,其中 x 是您要省略的列的索引。

    您可能还想将 TextView 上的 maxLines 设置为 1。

    【讨论】:

    • 感谢您的帮助,我不知道shrinkColumns。但是,我尝试过,由于某种原因,它使该行的高度非常高,而且屏幕宽度上的整行是第二列……我根本看不到第一列。第 2 列中的文本也没有以省略号结尾。有什么想法吗?
    • 2 是您的布局的正确值。您要缩小的那些 TextView 列位于第二个索引(第三个)列中。您从第二列开始,将 layout_column 设置为 1。这也是一个零索引属性。
    • 好的,我做了一个小调整,使用了android:stretchColumns="1"和android:shrinkColumns="2",一切看起来都不错。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2019-01-23
    • 2011-04-05
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 2019-06-28
    相关资源
    最近更新 更多