Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 带有长文本的TextView将其他视图推离屏幕大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试实现以下布局:
+----------------------------------------+
| [icon] [text] [icon]                   |
+----------------------------------------+
| [icon] [very loooooooooooooooooo [icon]|
|         oooooooooooooooong text]       |
+----------------------------------------+

当文本很短时,右边的图标需要在文本旁边(不是右对齐).当文本很长时,我需要包装文本.

我曾尝试使用LinearLayout和RelativeLayout,但是当我有一个长文本时,图标仍然被推出.这是我尝试过的布局:

LinearLayout中:

<LinearLayout xmlns:android="http://scheR_216_11845@as.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal">

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

    <TextView
        android:id="@+id/middle"
        android:text="a long long String,a long long String,"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:BACkground="#D0E198"/>

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

RelativeLayout的:

<RelativeLayout xmlns:android="http://scheR_216_11845@as.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

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

    <TextView
        android:id="@+id/middle"
        android:text="a long long String,"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:BACkground="#D0E198"
        android:layout_toRightOf="@id/left"/>

    <TextView
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right"
        android:layout_toRightOf="@id/middle"/>
</RelativeLayout>

在这两种情况下,右侧图标都会被推出屏幕.

我也尝试过LinearLayout,左右视图的layout_weight =“1”,中间视图的0.这会将两个图标都推离屏幕.

解决方法

以下是我为实现目标所做的工作:

我将layout_weight =“1”添加到中间TextView

<LinearLayout xmlns:android="http://scheR_216_11845@as.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal">

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

    <WrapWidthTextView
        android:id="@+id/middle"
        android:text="a long long String,"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:BACkground="#D0E198"
        android:layout_weight="1"/>

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

但是,当中间文本很短时,它仍将占据整个空的中间空间并将右侧TextView一直向右推.我必须继承TextView:

public class WrapWidthTextView
  extends TextView {

    // constructors here

    @Override protected void onMeasure (final int widthMeasureSpec,final int heightMeasureSpeC) {
        super.onMeasure (widthMeasureSpec,heightMeasureSpec);

        Layout layout = getLayout ();
        if (layout != null) {
            int width = (int) Math.ceil (getMaxLineWidth (layout))
                        + getCompoundPaddingLeft () + getCompoundPaddingRight ();
            int height = getMeasuredHeight ();
            setMeasuredDimension (width,height);
        }
    }

    private float getMaxLineWidth (Layout layout) {
        float max_width = 0.0f;
        int lines = layout.getLineCount ();
        for (int i = 0; i < lines; i++) {
            if (layout.getLineWidth (i) > max_width) {
                max_width = layout.getLineWidth (i);
            }
        }
        return max_width;
    }
}

使用WrapWidthTextView,当文本很短时,它将缩小视图以适合文本.

解决了我的问题.我希望这也有助于其他人.

大佬总结

以上是大佬教程为你收集整理的android – 带有长文本的TextView将其他视图推离屏幕全部内容,希望文章能够帮你解决android – 带有长文本的TextView将其他视图推离屏幕所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。