Android RelativeLayout布局与TextView底部对齐不';我不能正常工作

Android RelativeLayout布局与TextView底部对齐不';我不能正常工作,android,textview,android-relativelayout,Android,Textview,Android Relativelayout,尝试在高度可能不同的RelativeLayout容器中通过灵活的TextView进行对齐。 看起来很简单,但是,它并没有在任何应该对齐的地方对齐 下面是简单的代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_c

尝试在高度可能不同的RelativeLayout容器中通过灵活的TextView进行对齐。 看起来很简单,但是,它并没有在任何应该对齐的地方对齐

下面是简单的代码:

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

<-- Stands for any view that would force minimum height, e.g. an avatar image -->
<View
    android:layout_width="10dp"
    android:layout_height="73dp"
    android:background="#300f"/>

<TextView
    android:id="@+id/t1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:text="ceterum censeo carthaginem esse delendam"/>

<View
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/t1"
    android:layout_alignBottom="@+id/t1"
    android:background="#6f00"/>

</RelativeLayout>

结果是:

很明显,红色矩形会与文本垂直对齐。但事实并非如此


出了什么问题?..

用户android:layout_lower=“@id/id”


我希望它能起作用

尝试使用属性“android:layout\u below”。例如:
try using the attribute "android:layout_below". For example:
...
<View android:id="@+id/v1"
    android:layout_width="10dp"
    android:layout_height="73dp"
    android:background="#300f"/>

<TextView
    android:id="@+id/t1"
    android:layout_below="@+id/v1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:text="ceterum censeo carthaginem esse delendam"/>

...
... ...
请尝试下面的方法

<?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="wrap_content">
    <View android:id="@+id/some_view_id"
        android:layout_width="10dp"
        android:layout_height="73dp"
        android:background="#300f"/>

    <TextView
        android:id="@+id/t1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_below="@id/some_view_id"
        android:text="varying text view here that can \n go for multiple lines and not disturb the one below"/>

    <View
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/t1"
        android:background="#6f00"/>
</RelativeLayout>

尝试此代码,它可能会解决您的问题:

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

<View
    android:layout_width="10dp"
    android:layout_height="73dp"
    android:background="#300f" />

<TextView
    android:id="@+id/t1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:text="ceterum censeo carthaginem esse delendam" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <View
        android:layout_width="10dp"
        android:layout_height="73dp"
        android:background="#6f00" />
</LinearLayout>

</RelativeLayout>

像这样使用android:layout\u-to-endof

<TextView
    android:id="@+id/t1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:text="ceterum censeo carthaginem esse delendam"/>

<View
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@+id/t1"
    android:layout_alignBottom="@+id/t1"
    android:background="#6f00"/>

这与您有关。相对版面高度设置为包裹内容

从文件:

请注意,大小之间不能存在循环依赖关系 亲属关系及其子女的地位。比如你 不能具有高度设置为WRAP_内容的RelativeLayout和 将子项设置为与父项与底部对齐

所以,要么你把这个高度设置为与父母匹配,要么像我写的那样更简单
将android:layout\u centerVertical=“true”添加到红色布局中,尝试此解决方案。希望这有帮助

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


    <View
       android:layout_width="10dp"
       android:layout_height="73dp"
       android:background="#300f"/>
    <RelativeLayout 
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_centerVertical="true">

      <TextView
         android:id="@+id/t1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="ceterum censeo carthaginem esse delendam"/>

      <View
         android:layout_width="40dp"
         android:layout_height="wrap_content"
         android:layout_alignTop="@+id/t1"
         android:layout_alignBottom="@+id/t1"
         android:background="#6f00"/>
   </RelativeLayout>

 </RelativeLayout>



尝试使用下面的android:layout_=“@+id/t1”。将android:layout_centerVertical=“true”添加到红色中layout@Ahamed但愿如此!但我不能。它应该对齐,高度应该对齐,例如,当文本很长且线条变高时……您已将父布局高度设置为“包裹内容”,并将蓝色视图的高度设置为73dp。因此,父布局采用蓝色视图的高度。然后,由于您已经设置了属性android:centerVertical=“true”,文本视图将获得中心位置。所以文本视图得到了中心位置。然后将该值设置为红色视图的换行内容。因此,您要做的是,必须将属性值android:centerVertical=“true”设置为红色视图。否则,可以将父布局高度设置为与父布局匹配。然后您将看到“检查我的更新答案”对话框。让我知道如果它是工作,否则,我们将采取另一种解决方案绝对不是。。。它从下面开始,但是在错误的地方。这样做会让它消失。请。。。这是一个循环依赖。无论如何,它都不会工作,因为其目的是强制它以另一种方式工作。然后将textview和第二个视图包装成linearLayout视图,并将linearLayout视图设置在第一个视图的下方…duh!LinearLayout的性能成本很高,您不能将其他内容与它的子项对齐。这是一个简化的案例。长话短说,当你想在几个轴上对齐时,还不够好:-(显然不是,你不想把它放在下面,你想把它放在一条直线上,你的意思是,所有3条都在同一条直线上?我只是想把红色的一条的底部与文本的底部对齐。这就是alignBottom的全部内容:-)要结束的是水平约束!当您使用它时,红色矩形将与TextView对齐并放置在它后面,如果您希望它位于前面,请使用
android:layout\u toStartOf
现在水平位置无关紧要。我说的是垂直对齐,不是horizontal@Meymann...I无法清楚地确定您的确切问题是什么!。。。你能清楚地解释一下你的布局应该是什么样的吗?谢谢你的提示,文本视图居中,红色视图与底部对齐。伙计,没有循环依赖关系。文本视图已与中心对齐,红色视图与文本视图底部对齐。请阅读代码。不存在循环依赖关系。文本视图已与中心对齐,红色视图与文本视图底部对齐。就这么简单。因此,依赖项是:h(RelativeLayout)=h(蓝色视图),因为它的wrap_内容h(TextView)是由它的内容包装的。位置(文本视图)居中垂直h(红色视图)为绝对值。位置(红色视图)是文本视图的底部。就这样。没有循环依赖关系。依赖关系树是:RedView->TextView->RelativeLayout->BlueView Height(RelativeLayout)=max(children)就这么简单。谢谢。我也希望如此。这是我最初的尝试:(整个事实是,我必须将第二个内部视图放在ImageView中,而不是第二个内部视图(尽可能简化示例以解决问题).没有太多成功。我感觉RelativeLayout无法正确计算依赖关系,除非它们是直接的。它们应该这样做,因为它们会测量周期,然后定位。这应该解决wrap_content vs centerVertical伪相互依赖关系。可能需要深入研究Android的代码。这个解决方案适合你吗?不适合。请参阅我的注释。这是原始代码(使用ImageView)。我简化了它以解决问题。
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <View
       android:layout_width="10dp"
       android:layout_height="73dp"
       android:background="#300f"/>
    <RelativeLayout 
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_centerVertical="true">

      <TextView
         android:id="@+id/t1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="ceterum censeo carthaginem esse delendam"/>

      <View
         android:layout_width="40dp"
         android:layout_height="wrap_content"
         android:layout_alignTop="@+id/t1"
         android:layout_alignBottom="@+id/t1"
         android:background="#6f00"/>
   </RelativeLayout>

 </RelativeLayout>