Android TextView截断优先级或抗压缩性 [英] Android TextView Truncation Priority or Compression Resistance

查看:83
本文介绍了Android TextView截断优先级或抗压缩性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在屏幕上水平放置3个视图(固定大小的图像和2个单行文本视图:leftTextViewrightTextView),并且我试图使rightTextView紧靠屏幕leftTextView,但是如果两个标签的宽度都超过了屏幕尺寸,请截断leftTextiew.

I'm laying out 3 views horizontally on the screen (fixed-size image and 2 single-line text views: leftTextView and rightTextView) and I'm trying to get the rightTextView to hug against the leftTextView, but in the event that the width of both labels would exceed the screen size, truncate the leftTextiew.

所需功能示例:

|img|leftText|rightText|                ||(end of screen)
|img|leftTextMedium|rightText|          ||
|img|leftTextTooLongSoTrunc...|rightText||

实际发生的事情:

|img|leftText|rightText|                ||(end of screen)
|img|leftTextPrettyLongButNotHuge|rightT||
|img|leftTextWhichIsIncrediblyLongBlahBl||

当前,如果两个文本视图的大小都超过了视图的宽度,则即使我在leftTextView上设置了android:ellipsize="end"rightTextView最终仍会被压缩以腾出空间.

Currently, if the size of both text views exceeds the view's width, the rightTextView ends up getting squished down to make room, even though I've set android:ellipsize="end" on the leftTextView.

是否有一种方法可以使leftTextView具有截断优先级",该截断优先级表示如果将导致其他视图不适合,则应将其截断?或者在另一面,我可以将rightTextView设置为具有抗压缩性",以防止压缩它为leftTextView腾出空间吗?这是我的XML的示例:

Is there a way to make the leftTextView have a "truncation priority" that says it should truncate if it would cause other views to not fit? Or on the other side, can I set the rightTextView to have a "compression resistance" in order to keep it from being squished to make room for the leftTextView? Here's a sample of my XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/fixedWidthImage"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/leftTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/fixedWidthImage"
        android:layout_toRightOf="@id/fixedWidthImage"
        android:maxLines="1"
        android:textSize="18sp"
        android:ellipsize="end" />

    <TextView
        android:id="@+id/rightTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/leftTextView"
        android:layout_toRightOf="@+id/leftTextView"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:maxLines="1"
        android:textSize="12sp" />

</RelativeLayout>

我尝试过的事情:

  1. leftTextView中,添加toStartOf/toLeftOf rightTextView(崩溃应用程序)
  2. 而不是将rightTextView对齐到leftTextView的结尾/右边,而是将其翻转并将leftTextView对齐到rightTextView的开头/左边.这导致rightTextView锚定在屏幕的末尾,而不是直接锚定在leftTextView的右边.最终的外观示例:

  1. In the leftTextView, add a toStartOf / toLeftOf the rightTextView (crashes the app)
  2. Instead of aligning the rightTextView to the end/right of the leftTextView, flip it and align the leftTextView to the start/left of the rightTextView. This results in the rightTextView being anchored to the end of the screen instead of directly to the right of the leftTextView. Example of how this ends up looking:

|img|leftText                 |rightText||
|img|leftTextMedium           |rightText||
|img|leftTextTooLongSoTrunc...|rightText||

  • 即使我设置android:minWidth="25dp"只是为了确保至少可见rightTextView part (我也不愿意这样做,因为此文本长度是可变的),仍然最终会压缩宽度以为leftTextView腾出空间.

  • Even if I set android:minWidth="25dp" just to ensure at least part of the rightTextView is visible (which I don't want to do because this text length is variable) it still ends up compressing the width to make room for the leftTextView.

    推荐答案

    您可以通过TableLayoutshrinkColumns属性将此布局存档.

    You can archive this layout by TableLayout and shrinkColumns attribute.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        <!-- Row 1 -->
        <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:shrinkColumns="1">
    
            <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical">
    
                <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@mipmap/ic_launcher"/>
    
                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="4dp"
                        android:maxLines="1"
                        android:ellipsize="end"
                        android:text="leftText"/>
    
                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="4dp"
                        android:maxLines="1"
                        android:ellipsize="none"
                        android:text="rightText"/>
            </TableRow>
    
        </TableLayout>
    
    
        <!-- Row 2 -->
        <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:shrinkColumns="1">
    
            <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical">
    
                <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@mipmap/ic_launcher"/>
    
                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="4dp"
                        android:maxLines="1"
                        android:ellipsize="end"
                        android:text="leftTextPrettyLongButNotHuge"/>
    
                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="4dp"
                        android:maxLines="1"
                        android:ellipsize="none"
                        android:text="rightText"/>
            </TableRow>
    
        </TableLayout>
    
        <!-- Row 3 -->
        <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:shrinkColumns="1">
    
            <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical">
    
                <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@mipmap/ic_launcher"/>
    
                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="4dp"
                        android:maxLines="1"
                        android:ellipsize="end"
                        android:text="leftTextWhichIsIncrediblyLongBlahBlahBlahBlah"/>
    
                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="4dp"
                        android:maxLines="1"
                        android:ellipsize="none"
                        android:text="rightText"/>
            </TableRow>
    
        </TableLayout>
    
    </LinearLayout>
    

    这篇关于Android TextView截断优先级或抗压缩性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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