Android relativeLayout设置为使用alignParentRight包装内容?
我想将相对布局设置为包装内容,并将所有子项向右对齐,这里是xml:Android relativeLayout设置为使用alignParentRight包装内容?,android,android-layout,android-relativelayout,right-align,Android,Android Layout,Android Relativelayout,Right Align,我想将相对布局设置为包装内容,并将所有子项向右对齐,这里是xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativeLayout18" android:layout_width="wrap_content" android:layout_height="wrap_content" android:backgr
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000">
<TextView
android:id="@+id/textViewMsgDate"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="right"
android:layout_marginRight="8dp"
android:gravity="center"
android:textColor="#ffff"
android:text="simple"
/>
</RelativeLayout>
但正如你在照片中看到的,我没有成功?虽然如果我对齐到左换行内容,效果会如预期一样。这里的问题是您将RelativeLayout的子项对齐到其父项(RelativeLayout)的右侧,因为RelativeLayout的宽度设置为换行内容,所以不会移动子项 如果不想将子对象与父对象右侧对齐,则父对象的宽度应大于其子对象的宽度。因此,要么给RelativeLayout一些宽度,要么使其“匹配父对象”,要么将RelativeLayout自身向右对齐,以便其子对象(TextView)随它一起移动 注意:如果向左对齐,并将RelativeLayout宽度设置为“wrap_content”,则其行为与您预期的一样,因为RelativeLayout已向左对齐,因此它也是子对象 已编辑 要实现您的目标,只需执行以下操作:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout18"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/textview_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#000">
<TextView
android:id="@+id/textViewMsgDate"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginRight="8dp"
android:textColor="#ffff"
android:text="simple"
/>
</RelativeLayout></RelativeLayout>
结果应该是这样的:
谢谢您的回答。但我不想将其设置为与父项匹配,因为我不希望背景黑色填充所有窗口。我已编辑了我的答案,并为您的案例添加了解决方案。它应该有用:)再次谢谢你。但它仍然不起作用。文本没有向右对齐!您的意思是文本视图没有向右对齐,还是文本本身没有对齐?如果你说的是文本,那么很可能是因为你给了文本一个8dp的右边距,我指的是文本视图。一分钟后,relativeLayout.layoutParams具有布局重力。因为我没有在文档中找到它