在我的 Android 应用程序的活动布局中,我需要在其右侧对齐一行文本,如下所示:

enter image description here

到目前为止,这是我的 XML:

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <View
                android:id="@+id/line"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_centerVertical="true"
                android:background="@android:color/darker_gray"/>


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" Text "
                android:layout_alignRight="@id/line"/>


        </RelativeLayout>

基本上问题是线的宽度根据屏幕的宽度和文本的宽度而改变。它应该是屏幕的宽度减去文本的宽度。

在我的 XML 文件中,线条的宽度等于屏幕的宽度,并且文本覆盖在右侧对齐的线条上。这不是我想要的。一种想法是使我的文本背景与活动背景相同。但是,我不确定这是否是最好的主意。

我想知道是否有其他方法可以解决Android设计中的此类问题。

有帮助吗?

解决方案

你可以创建一个 layout_weight 并包裹你的视图和 textView 里面一个 linearlayout 启用重量属性

样本:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <View
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="3dp"
        android:layout_gravity="center_vertical"
        android:background="@android:color/darker_gray" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" Text " />

</LinearLayout>

其他提示

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text=" Text " />

<View
    android:id="@+id/line"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/textView1"
    android:background="@android:color/darker_gray" />
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top