如果文本太长,请将两个并排的文本视图移动到另一个文本视图下



我有两个类似的文本视图:

=======================
= TextView1 TextView2 =
=======================

我想检测一下,当文本视图太长时,它们会显示为这样:

=======================
=      TextView1      =
=      TextView2      =
=======================

目前,对于较长的文本,它显示如下:

=======================
= TextView1 Text      =
=           View2     =
=======================

我如何做到这一点,当文本很短时,文本视图并排,当文本太长时,第二个文本视图不会被拆分,而是移动到第二行?

我很难找到一个创建单个文本视图并根据长度构建文本的解决方案(如果短,则为文本1+填充+文本2,如果长,则为文字1+"\n"+文本2),但我不喜欢这个解决方案。

是否有任何方法可以检测第二个文本是否会被拆分,从而将包含文本视图的布局的方向从水平更改为垂直?

更新

这是我的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:gravity="center">
    <TextView
        android:id="@+id/my_text_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:padding="10dp"
        android:text="@string/text1"/>
    <TextView
        android:id="@+id/my_text_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="5dp"/>
</LinearLayout>

我找到了一个更好的解决方案。将我的文本视图更改为可自动调整大小的文本视图(更多信息请点击此处)

此外,每个文本视图都在一个单独的布局中,以确保两个文本视图的大小调整为相同的值。我的xml如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:id="@+id/value_linear_layout"
              android:gravity="center">
    <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content">
        <com.mihaela.view.AutoResizeTextView
            android:id="@+id/my_text_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content">
        <com.mihaela.view.AutoResizeTextView
            android:id="@+id/my_text_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

我已经实现了AutoResizeTextView中的OnTextResizeListener来实现这一点:

public class TextWidthResizeListener implements OnTextResizeListener {
    @Override
    public void onTextResize(TextView textView, float oldSize, float newSize) {
        TextPaint paint = textView.getPaint();
        if (paint.measureText(textView.getText().toString()) > (valueLinearLayout.getWidth() / 2)){
            valueLinearLayout.setOrientation(LinearLayout.VERTICAL);
        }
    }
}

其中valueLinearLayout为:

valueLinearLayout = (LinearLayout)findViewById(R.id.value_linear_layout);

这个解决方案最适合我,因为文本视图是并排标注的,直到达到最小尺寸。当达到最小大小,但文本仍然不合适时,文本视图将一个接一个地对齐。

此外,侦听器的这个想法也可以应用于不可调整大小的文本视图。我将把这个答案定为正确答案。

您应该使用一个多行的TextView,并按如下方式设置文本:

mTextView.setText(text1+" "+text2);

mTextView.setText(text1+"n"+text2);

取决于您的特殊需求。

编辑:您可以在html中指定文本,然后使用Html.fromHtml(htmlString)并在TextView中显示此文本。

 String text1 ="<font color="red">This is some text!</font>"
 String text2="<font color="blue">This is some other text!</font>"
 textView.setText(Html.fromHtml(text1+ "<br/>"+ text2);

我对接受的答案做了一个稍微不同的版本。我没有以任何方式更改布局xml,也没有使用onTextResize()AutoResizeTextView,因为这对我的情况来说似乎有些过头了。如果设备的语言设置导致使用长字符串,我需要我的LinearLayout从水平方向切换到垂直方向。

布局

<LinearLayout
    android:id="@+id/customer_care_bottom_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:layout_marginBottom="@dimen/lmargin_bottom_10">
    <TextView
        android:id="@+id/customer_care_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/CUSTOMER_CARE_TITLE" />
    <TextView
        android:id="@+id/customer_care_number_information"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/CUSTOMER_CARE_INFORMATION"/>
</LinearLayout>

Java

private void setCustomerServiceLayoutOrientationBasedOnTextWidth() {
        TextPaint paint = customer_care_number_text.getPaint();
        TextView tvCustomerCareTitle = (TextView) findViewById(R.id.customer_care_title);
        TextView tvCustomerCareInformation = (TextView) findViewById(R.id.customer_care_information);
        int halfCustomerServiceLayoutWidth = getScreenWidth() / 2;
        boolean isCustomerCareTitleTooLong = paint.measureText(tvCustomerCareTitle.getText().toString()) > customerServiceLayoutWidth;
        boolean isCustomerCareInformationTooLong = paint.measureText(tvCustomerCareInformation.getText().toString) > customerServiceLayoutWidth;
        if (isCustomerCareTitleTooLong || isCustomerCareInformationTooLong) {
            LinearLayout llCustomerCareBottom = (LinearLayout) findViewById(R.id.customer_care_bottom_layout);
            llCustomerCareBottom.setOrientation(LinearLayout.VERTICAL);
        }
    }
private int getScreenWidth() {
    int screenWidth;Display display = getWindowManager().getDefaultDisplay();
    if (Build.VERSION.SDK_INT < 13) {
        screenWidth = display.getWidth();
    } else {
        Point point = new Point();
        display.getSize(point);
        screenWidth = point.x;
    }
    return screenWidth;
}

最新更新