将两个并排textviews是逐个排列,如果文字太长 [英] Move two side by side textviews to be one under another if text is too long

查看:142
本文介绍了将两个并排textviews是逐个排列,如果文字太长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个textviews是这样的:

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

和我想检测时textviews太长,使得它们显示的是这样的:

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

目前对于较长的文本,它会显示这样的:

  =======================
= TextView1文本=
=视图2 =
=======================
 

我怎么能做到这一点,这样当文本短textviews并排而当它太长,第二个TextView的不是分裂,但移动到第二行?

我因子评分在一个解决方案来创建一个单一的TextView,并根据长度构建文本(文本1 +填充+文本2,如果短,和文本1 +\ N+文本2如果长时间),但我不喜欢该溶液中。

有什么方法来检测,如果第二个文本将被分割,从而改变包含从水平立方米的textviews布局的方向垂直?

更新

这是我的xml:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
              机器人:layout_width =FILL_PARENT
              机器人:layout_height =FILL_PARENT
              机器人:重力=中心>

    <的TextView
        机器人:ID =@ + ID / my_text_1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =center_vertical
        机器人:填充=10dp
        机器人:文本=@字符串/文本1/>

    <的TextView
        机器人:ID =@ + ID / my_text_2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =center_vertical
        机器人:layout_marginRight =5DP/>

< / LinearLayout中>
 

解决方案

我已经找到了更好的解决方案。改变了我的textviews到autoresizable textviews(更多信息<一个href="http://stackoverflow.com/questions/5033012/auto-scale-textview-text-to-fit-within-bounds">here)

而且,每个TextView的是在一个单独的布局,以确保这两个textviews的大小被调整为相同的值。 我的XML看起来是这样的:

 &LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
              机器人:layout_width =FILL_PARENT
              机器人:layout_height =FILL_PARENT
              机器人:ID =@ + ID / value_linear_layout
              机器人:重力=中心&GT;

    &LT;的LinearLayout
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_weight =1
            机器人:layout_height =WRAP_CONTENT&GT;
        &LT; com.mihaela.view.AutoResizeTextView
            机器人:ID =@ + ID / my_text_1
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT/&GT;
    &LT; / LinearLayout中&GT;

    &LT;的LinearLayout
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_weight =1
            机器人:layout_height =WRAP_CONTENT&GT;

        &LT; com.mihaela.view.AutoResizeTextView
            机器人:ID =@ + ID / my_text_2
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT/&GT;
    &LT; / LinearLayout中&GT;

&LT; / LinearLayout中&GT;
 

和我已经实现了 OnTextResizeListener AutoResizeTextView 要做到这一点:

 公共类TextWidthResizeListener实现OnTextResizeListener {

    @覆盖
    公共无效onTextResize(TextView的TextView的,浮动oldSize,浮newSize){
        TextPaint漆= textView.getPaint();
        如果(paint.measureText(textView.getText()的toString())&GT;(valueLinearLayout.getWidth()/ 2)){
            valueLinearLayout.setOrientation(LinearLayout.VERTICAL);
        }
    }
}
 

在这里valueLinearLayout是:

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

该解决方案最适合我,因为当他们并肩,直到最小尺寸的textviews的尺寸。当最小大小为止,文本还是放不下,该textviews将对准之一的另一回事。

此外,这种想法与侦听可应用于不可调整大小textviews也。 我要把这个答案是正确的。

I have two textviews like this:

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

And I would like to detect when the textviews are too long such that they are displayed like this:

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

currently for longer text, it is displayed like this:

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

how can I do this, such that when the text is short the textviews are side by side and when it is too long, the second textview is not splitted but moved to the second line?

I tought at a solution to create a single textview and build the text according to length (text 1 + padding + text 2 if short, and text 1 + "\n" + text 2 if long) but I do not like this solution.

Is there any way to detect if the second text will be split such that to change the orientation of the layout that contains the textviews from horizontal cu vertical?

UPDATE

This is my 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>

解决方案

I have found a better solution. Changed my textviews into autoresizable textviews (more info here)

Also, each textview is in a separate layout, to make sure both textviews are resized to the same value. My xml looks like this:

<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>

and I have implemented the OnTextResizeListener from AutoResizeTextView to do this:

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);
        }
    }
}

where valueLinearLayout is:

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

This solution best fits for me, as the textviews are dimensioned when they are side by side until a minimum size. When the minimum size is reached, and the text still does not fit, the textviews will be aligned one under another.

Also, this idea with the listener can be applied to non-resizable textviews also. I will set this answer as the correct one.

这篇关于将两个并排textviews是逐个排列,如果文字太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆