【问题标题】:RelativeLayout with two textviews on one horizontal line with ellipsis support一条水平线上有两个文本视图的相对布局,支持省略号
【发布时间】:2015-04-23 04:37:20
【问题描述】:

我正在尝试设置一个 RelativeLayout,其中两个 TextView 显示在一行上。第一个 TextView 应该左对齐。第二个 TextView 应该向右对齐。如果第二个 TextView 文本会扩展以与第一个 TextView 重叠,那么我想省略中间的文本。我不想省略第一个 TextView。

这是我想出的。问题是第二个 TextView 直接位于第一个 TextView 的右侧,而不是与最右侧对齐。好像我不能同时使用 alignParentRight 和 toRightOf。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/Left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="[DetailedType]"/>
    <TextView
        android:id="@+id/Right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/Left"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:ellipsize="middle"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="[CustomerName]"/>
</RelativeLayout>

【问题讨论】:

    标签: android android-layout xamarin.android


    【解决方案1】:

    实现这个id的基本思路是在RelativeLayout的水平中心添加一个虚拟的不可见视图,并告诉左边TextView在它的左边,右边TextView在它的左边就在它的右边……见下文

    关于向右对齐的问题,您应该将 TextView 拉伸到屏幕的一半,然后在 TextView 上设置 android:gravity="right"

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
        <View
            android:id="@+id/dummy"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            />
    
        <TextView
            android:id="@+id/Left"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@id/dummy"
            android:text="[DetailedType]"
            android:textAppearance="?android:attr/textAppearanceMedium"/>
    
    
        <TextView
            android:id="@+id/Right"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@id/Left"
            android:gravity="right"
            android:ellipsize="middle"
            android:singleLine="true"
            android:text="[CustomerName]"
            android:textAppearance="?android:attr/textAppearanceMedium"/>
    </RelativeLayout>
    

    但是你总是可以使用LinerLayout 来工作

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    
    
        <TextView
            android:id="@+id/Left"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="[DetailedType]"
            android:textAppearance="?android:attr/textAppearanceMedium"/>
    
    
        <TextView
            android:id="@+id/Right"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ellipsize="middle"
            android:singleLine="true"
            android:text="[CustomerName]"
            android:textAppearance="?android:attr/textAppearanceMedium"/>
    </LinearLayout>
    

    【讨论】:

    • 有什么办法可以让右边的TextView一直展开到左边TextView的末尾?就目前而言,当文本视图越过控件的水平中心时,它会省略文本。如果右侧文本与左侧文本重叠,我只希望文本变椭圆。如果右侧文本与视图的水平中心重叠,则现在它正在省略文本。
    【解决方案2】:

    据我了解,您只会在右视图上错过android:gravity="right"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-27
      • 2020-10-21
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-19
      相关资源
      最近更新 更多