在我的 Android 应用程序 Activity 的布局中,我需要一行文本在其右侧对齐,如下所示:
到目前为止,这是我的 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 文件中,线条的宽度等于屏幕的宽度,文本覆盖在右侧对齐的线条上。这不是我想要的。 一个想法是让我的文本背景等同于 Activity 的背景。但是,我不确定这是否是最好的主意。
我想知道是否有任何其他方法可以解决 Android 设计中的此类问题。
最佳答案
您可以创建一个 layout_weight
并将您的 View 和 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>
关于android - 如何在android相对布局中根据屏幕宽度为 View 设置可缩放宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25026434/