在android相对布局中,如何根据视图的屏幕宽度设置可缩放的宽度
在我的Android应用程序的活动布局中,我需要在右侧有一行文字对齐,如下所示: 到目前为止,这是我的XML:在android相对布局中,如何根据视图的屏幕宽度设置可缩放的宽度,android,android-layout,android-relativelayout,Android,Android Layout,Android Relativelayout,在我的Android应用程序的活动布局中,我需要在右侧有一行文字对齐,如下所示: 到目前为止,这是我的XML: <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:id="@+id/line"
<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设计中是否有其他方法来解决这些问题
<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" />
您可以创建一个
布局_weight
并将视图和文本视图
包装在线性布局中
以启用权重属性
示例:
<?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>
不适合我。它画了一条线,把文本视图放在第二行。谢谢,它成功了。你为什么把重量设为3?为什么没有其他号码?