简体   繁体   English

当屏幕宽度较小时,如何在右对齐按钮旁边省略居中对齐的文本视图

[英]How to ellipsize center-aligned textview beside a right-aligned button when screen width is small

------------------------------------
|           LONG_TITL...   |button|  |
|------------------------------------|
|                                    |

I have an app header which consists of the app title and a button. 我有一个由应用程序标题和按钮组成的应用程序标头。 The button should be fixed on the right side but I want the title to be in the middle and ellipsize when the screen is too small. 该按钮应该固定在右侧,但是当屏幕太小时,我希望标题位于中间并且为椭圆形。 Problem is the title just won't ellipsize even though the button is nearly beside it. 问题是,即使按钮几乎在标题旁边,标题也不会省略。

Here's what I have so far: 这是我到目前为止的内容:

<RelativeLayout android:id="@+id/headr"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

    <ImageView
        android:id="@+id/bar_orange"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:minHeight="40dp"
        android:scaleType="centerCrop"
        android:src="@drawable/bar_orange"/>

    <TextView
        android:id="@+id/Title"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="@string/bcd"
        android:textColor="#FFFFFF"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:textSize="18sp"
        android:textStyle="bold"/>

    <Button
        android:padding="5dp"
        android:id="@+id/home"
        android:textColor="#ffffff"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:singleLine="true"
        android:background="@drawable/button_home"
        android:text="@string/home"/>

</RelativeLayout>

Is there a way to do this with just RelativeLayout ? 是否只有RelativeLayout才能做到这一点?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" 
        >

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_toLeftOf="@+id/button"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="very large center align text very large center align text very large center align text" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Right button" />
    </RelativeLayout>

</RelativeLayout>

add this to your textView 将此添加到您的textView

  android:layout_centerHorizontal="true"
  android:layout_centerVertical="true"

在TextView中更改为android:ellipsize="end"

为TextView设置固定宽度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM