【问题标题】:Ellipsize not working in this table layoutEllipsize 在此表格布局中不起作用
【发布时间】:2012-04-06 02:19:21
【问题描述】:

我目前正在尝试学习 Android。几天来一直在阅读教程和手册。 我遇到了布局问题。

我正在从网页中抓取内容并将其显示给用户。这就是我的应用程序的工作原理。

我的布局如下:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:padding="5px">

<TextView
    android:id="@+id/tvOutput"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_marginTop="5dp">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:stretchColumns="0" android:id="@+id/tblOutput">

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </TableRow>

            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </TableRow>

            <TableRow
                android:id="@+id/tableRow3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </TableRow>

            <TableRow
                android:id="@+id/tableRow4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </TableRow>

            <TableRow
                android:id="@+id/tableRow5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </TableRow>

            <TableRow
                android:id="@+id/tableRow6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </TableRow>

            <TableRow
                android:id="@+id/tableRow7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </TableRow>

            <TableRow
                android:id="@+id/tableRow8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </TableRow>

            <TableRow
                android:id="@+id/tableRow9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </TableRow>

            <TableRow
                android:id="@+id/tableRow10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </TableRow>

            <TableRow
                android:id="@+id/tableRow11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </TableRow>

        </TableLayout>

        <Button
            android:id="@+id/btnBack"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/lblBack" android:layout_marginTop="10dip"/>


        <Button
            android:id="@+id/btnSendSMS"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="@string/lblSendSMS" />

    </LinearLayout>

</ScrollView>

通过代码,我将遍历 TableRows 并分别添加两个 TextView。下面是它的一个示例:

                for(i=1;i<=11;i++){
                    TextView tv1 = new TextView(getApplicationContext());
                    TextView tv2 = new TextView(getApplicationContext());

                    //get values and store it in strValue1 & strValue2
                    //....

                    tv1.setText(strValue1); //a long text
                    tv2.setText(strValue2); //would always be a 3 letter value
                    //tv1.setEllipsize(TruncateAt.MARQUEE);
                    //tv1.setMarqueeRepeatLimit(1);
                    tv1.setSingleLine(true);
                    tv1.setEllipsize(TruncateAt.END);
                    tv1.setGravity(Gravity.LEFT);


                    tr = (TableRow) findViewById( getResources().getIdentifier("tableRow"+i, "id", getPackageName()));
                    tr.addView(tv1);
                    tr.addView(tv2);
                }

问题是,每个 TableRow 上第一个 TextView 的 setEllipsize() 没有做任何事情。它只显示第一个TextView的文本(长文本溢出宽度),甚至不显示TextView2。

当我在现有 TextView(我的布局中的第一个 TextView - 您可以从上面的 XML 中看到,它位于表格布局上方)上尝试 setEllipsize() 和 setSingleLine() 时,它工作正常。

任何想法我哪里出错了?或者有什么建议?

提前致谢:)

【问题讨论】:

    标签: android layout


    【解决方案1】:

    您需要使用任何一种可能的方式为 TextView 设置固定高度。设置行数或仅设置 TextView 布局的高度。如果您不这样做,那么由于 ure textview 将 wrap_content 作为高度,它将调整大小并显示所有文本。

    所以先固定textview的大小,然后setellipsize()

    【讨论】:

    • 谢谢 :) 我尝试使用 setMaxLines(1) 设置 TextView 的最大行数。但这行不通。我将尝试将Layout_Height 设置为fill_parent。为此,我必须创建一个 TableLayout 参数并向其中添加规则?
    • 也可以使用宽度。有时在换行内容中,宽度会根据内容发生变化,会出现此问题。
    • 我尝试将 fill_parent 设置为 TextViews 的所有父级。但这没有用。现在尝试使用宽度。要为位于 TableRow(TableLayout 内)中的 TextView 设置布局参数(FILL_PARENT),我是否必须创建一个 TableLayout 对象并将其传递给 TextView 的 setLayoutParams ?
    • 我也尝试过分离 TableRow 布局然后通过代码膨胀的想法,基于此解决方案:stackoverflow.com/questions/2684775/… 在行的布局中,我添加了两个 TextView,然后设置对于第一个 TextView,LayoutWidth 为 FILL_PARENT,Gravity 为 LEFT,SingLines 为 TRUE。但它的显示方式与以前相同。 :(
    • 你能不能把 TextWidth 改成 100 dpi 然后试试看?我注意到你有一个滚动视图,它可以让组件调整大小。并且 fill_parent 没有帮助,因为它在滚动视图中。因此,将您的宽度和高度固定为 100x72dpi(大约 2 行高)
    猜你喜欢
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    • 2018-05-30
    相关资源
    最近更新 更多