【问题标题】:Move two side by side textviews to be one under another if text is too long如果文本太长,将两个并排的文本视图移动到另一个下方
【发布时间】:2015-05-28 07:51:55
【问题描述】:

我有两个这样的文本视图:

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

我想检测文本视图何时太长以至于它们显示如下:

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

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

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

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

我提出了一个解决方案来创建单个文本视图并根据长度构建文本(文本 1 + 填充 + 文本 2 如果短,文本 1 + "\n" + 文本 2 如果长)但我不喜欢这个解决方案。

有什么方法可以检测第二个文本是否会被拆分,从而改变包含 textviews 的布局的方向从水平 cu 垂直?

更新

这是我的 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 更新了问题。我需要显示两个通常并排显示的文本,但是如果它们太长,则应该在另一个下显示它们。这是我被要求做的。
  • 它们真的需要分开,或者从第二行的左边距开始的第二个文本的简单换行就足够了吗?
  • 不,没有办法从 xml 实现这一点。您可以以某种方式以编程方式测量您的text1 并根据text1 的大小和设备宽度从代码中添加您的TextView。但这将比仅使用 text1+" " text2 复杂得多。

标签: android android-layout textview


【解决方案1】:

我找到了更好的解决方案。将我的文本视图更改为可自动调整大小的文本视图(更多信息 here

此外,每个文本视图都在一个单独的布局中,以确保两个文本视图的大小都调整为相同的值。 我的 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);

这个解决方案最适合我,因为当它们并排时,文本视图的尺寸是最小的。当达到最小尺寸时,文本仍然放不下,文本视图将在另一个下对齐。

此外,这种带有监听器的想法也可以应用于不可调整大小的文本视图。 我会把这个答案设为正确的。

【讨论】:

  • 我正在尝试您的解决方案,但我只是从您链接到的其他答案中获得椭圆、调整大小的结果。你把你的 TextWidthResizeListener 类放在哪里,你怎么称呼它?
【解决方案2】:

您应该使用单行多行 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);

【讨论】:

  • 这对我来说不是一个真正的选择,因为我在 textviews 上有不同的样式、不同的文本颜色等......
  • 我尝试了一种不同的方法来使用可调整大小的文本视图并检查文本何时最小化以将布局方向更改为垂直,但我没有设法使其工作,因为 onDraw() 方法没有触发任何 layoutchangelisteners 和 getTextSize 在 onDraw 更改之前返回文本的大小。所以,我认为你的解决方案仍然是唯一的选择。谢谢。
【解决方案3】:

我对接受的答案做了一个稍微不同的版本。我没有以任何方式更改我的布局 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;
}

【讨论】:

    猜你喜欢
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多