开发者

Force TextView to mutiline without \n

Any idea how I can force the textview to go to a new line once it runs out of space inside the view. The behaviour I want to happen is that without programatically findout out the size and forcing the newline I want it to happen by iteself.

In this code it forces the buttons off the screen.

<TableRow>
                <TextView android:layout_width="fill_parent"
                    android:paddingLeft="5dp" android:paddingRight="5dp"
                    android:singleLine="false" android:layout_height="wrap_content"
                    style="@style/heading1" android:gravity="left" android:id="@+id/store_address"></TextView>

                <TableLayout android:layout_height="wrap_content"
                    android:layout_width="wrap_content" android:gravity="right">
                    <TableRow>
                        <ImageView android:id="@+id/img_user" android:gravity="right"
                            android:layout_width="wrap_content" android:layout_height="wrap_content"
                            android:scaleType="fitXY" android:src="@drawable/qrcode"
                      开发者_如何转开发      android:paddingBottom="2dp" />
                    </TableRow>
                    <TableRow>
                        <ImageView android:id="@+id/nfc" android:gravity="right"
                            android:layout_width="wrap_content" android:layout_height="wrap_content"
                            android:scaleType="fitXY" android:src="@drawable/nfc" />
                    </TableRow>
                </TableLayout>
            </TableRow>


add the following attribute to the TextView control:

android:maxWidth = "100dip" Here i have used 100dip, you can give whatever dimensions you want. Once the text exceeds the specified width, it will automatically be shifted to a new line.


This is not the most dynamic way to handle the problem, but I increased the Height of the TextView depending on how many lines were being drawn.

        // Create TextViews with Question
        TextView question = new TextView(this);

        if (("Q" + i + ". " + alQuestions.get(i-1)).length() < 70)
        {
            question.setLayoutParams(new TableRow.LayoutParams(0, 75, .9f));
        }
        else if (("Q" + i + ". " + alQuestions.get(i-1)).length() < 110)
        {
            question.setLayoutParams(new TableRow.LayoutParams(0, 100, .9f));
        }
        else if (("Q" + i + ". " + alQuestions.get(i-1)).length() < 150)
        {
            question.setLayoutParams(new TableRow.LayoutParams(0, 125, .9f));
        }

        question.setTextColor(Color.BLUE);
        question.setSingleLine(false);
        question.setEllipsize(TextUtils.TruncateAt.END);
        question.setMaxLines(5);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜