【问题标题】:android make left textview to be shorten and right remain fully visibleandroid使左侧文本视图缩短,右侧保持完全可见
【发布时间】:2014-01-01 12:28:48
【问题描述】:

我需要水平显示两个单行 TextView。左侧的 TextView(我们将其命名为 1)可能有更长的文本,可能会缩短并以“...”结尾。右文本视图 (2) 的文本很短,不应缩短。

我希望 1 保持与父级的左端对齐。 2 与 1 的右侧对齐。

现在我必须满足两个条件

a) 如果 1 有一个短文本,那么 2 应该对齐到 1 的右侧(没有一个被缩短)。

b) 但如果 1 的文本太长,则 1 的文本应缩短 '...' 而视图 2 最大程度地移动到父级的右侧,但仍保持完全可见(没有 .. .)

我目前的解决方案如下。场景 b) 对我来说很好,但在 a) 的情况下,问题是视图 2 移动到父视图的右侧,视图 1 移动到左侧 - 两者都很短,并且两者之间有很大的空间看起来很奇怪。我想让 2 向左移动(在 1 旁边)并将这个空间留在右侧。

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_marginTop="1dp"
    android:layout_weight="0.5">

    <TextView
        android:id="@+id/ns_in_txt"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:gravity="top|left"
        android:maxLines="1"
        android:singleLine="true"
        android:textColor="#00a2ff"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/ns_txt"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/ns_in_txt"
        android:gravity="top|left"
        android:maxLines="1"
        android:singleLine="true"
        android:textColor="#00a2ff"
        android:textSize="18sp" />
</RelativeLayout>

【问题讨论】:

  • 也许我找到了解决您问题的方法,请查看我的编辑答案。问候。

标签: android textview android-relativelayout short


【解决方案1】:

我可以建议使用LinearLayout 和一点编码的组合。我们的想法是让它们并排放置,无论大小。并在测量和布局右侧 textview 后,将左侧 textview 的最大宽度设置为剩余的任何空间。

这是布局文件,这里没什么特别的:

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/ns_in_txt"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:ellipsize="end"
        android:gravity="top|left"
        android:maxLines="1"
        android:singleLine="true"
        android:textColor="#00a2ff"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/ns_txt"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:ellipsize="none"
        android:gravity="top|left"
        android:maxLines="1"
        android:singleLine="true"
        android:textColor="#00a2ff"
        android:textSize="18sp" />

</LinearLayout>

并向活动/片段添加一些代码:

final TextView tvLeft = (TextView) findViewById(R.id.ns_txt);
final TextView tvRight = (TextView) findViewById(R.id.ns_in_txt);
ViewTreeObserver obs = tvRight.getViewTreeObserver();
obs.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
    public void onGlobalLayout() {
        tvLeft.setMaxWidth(SCREEN_WIDTH - tvRight.getWidth());
    }
});

【讨论】:

    【解决方案2】:

    显然,您需要两种情况,需要为父布局设置不同的方向:第一个是水平的,第二个是垂直的。也许我错了(我希望如此,但是)在静态 xml 中,这样做会很困难。

    试试下面的代码来测试我是否错了:

    场景1:方向水平=文字1不够大

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="1dp"
        android:orientation="horizontal" >
    
        <TextView
            android:id="@+id/textLong"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:ellipsize="end"
            android:singleLine="true"
            android:textColor="#00a2ff"
            android:textSize="18sp"
            android:text="This is a normal text not big" />
    
        <TextView
            android:id="@+id/textShort"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:singleLine="true"
            android:textColor="#00a2ff"
            android:textSize="18sp"
            android:text="short text" />
    
    </LinearLayout>  
    

    场景2:方向垂直=文字1太大

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="1dp"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/textLong"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:ellipsize="end"
            android:singleLine="true"
            android:textColor="#00a2ff"
            android:textSize="18sp"
            android:text="This is a biiiggg loooonnng teeeeexxxxxtttt" />
    
        <TextView
            android:id="@+id/textShort"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:singleLine="true"
            android:textColor="#00a2ff"
            android:textSize="18sp"
            android:text="short text" />
    
    </LinearLayout>  
    

    要解决您的问题,您可以尝试 2 个解决方案。

    首先,尝试创建一个maxLenght 限制,该限制在您的Activity 中计算并更改LinearLayout 的父方向。获取您拥有的字符数并同时显示方向。
    其次,自定义自己的类扩展TextView。并创建一个getWidth 方法,该方法返回长TextView 与其父级相比的宽度并更改方向。

    也许以下问题/答案可能有用(我认为没有解决方案,但更多的是灵感):
    In Android how to get the width of the Textview which is set to Wrap_Content
    Get the size of a text in TextView
    How to find android TextView number of characters per line?
    Auto Scale TextView Text to Fit within Bounds
    /p>


    编辑:

    我找到了我上面写的最后一个网址的解决方案。在开发人员想要与您相同的地方查看此答案。所以他决定创建一个autoresizable textview。看这里:Move two side by side textviews to be one under another if text is too long

    希望对你有所帮助。

    【讨论】:

    • 这是一个好主意,直​​到最后都没有测试,因为我从 kimikanen 查找了一个,它立即工作,无需对源代码进行太多更改。无论如何,非常感谢您花费的时间和精力:)
    【解决方案3】:

    尝试这样做

    <RelativeLayout
         android:orientation="horizontal"
         android:layout_width="fill_parent"
         android:layout_height="wrap_context"
         android:layout_marginTop="1dp">
    
         <TextView
              android:id="@+id/ns_in_txt"
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:layout_alignParentRight="true"
              android:gravity="left"
              android:maxLines="1"
              android:ellipsize="end"
              android:singleLine="true"
              android:textColor="#00a2ff"
              android:textSize="18sp" />
    
         <TextView
              android:id="@+id/ns_txt"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:layout_alignParentLeft="true"
              android:layout_toLeftOf="@id/ns_in_txt"
              android:gravity="left"
              android:maxLines="1"
              android:singleLine="true"
              android:textColor="#00a2ff"
              android:textSize="18sp" />
    
    </RelativeLayout>
    

    【讨论】:

    • 我认为这条线 android:layout_alignParentRight="true" 会让事情出错... ns_in_txt 的条件应该是首先最大限度地向左然后不超过父...不能同时实现两个..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 2022-01-19
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    相关资源
    最近更新 更多