android相对布局不能正常工作

时间:2015-11-12 06:17:58

标签: android xml android-layout

我正在尝试为recyclerview做一行,行将分为3列

  1. 首先 - 图像按钮
  2. 第二内容(textviews)
  3. 第三 - 图像按钮
  4. 当我加载一个巨大的内容时,它会使布局增长,并且没问题,但我希望侧面板也能在高度上增长:

    我想要什么(和Android Studio SHOW一样(liar =()):

    enter image description here

    以及我真正得到的:

    enter image description here

    我的row.xml是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
      <RelativeLayout
          android:id="@+id/thebig_father_layout"
          android:layout_width="match_parent"
          android:background="@android:color/holo_blue_bright"
          android:layout_height="match_parent">
    
    
          <RelativeLayout
              android:background="@android:color/holo_red_dark"
              android:layout_alignParentLeft="true"
              android:gravity="center_vertical"
              android:id="@+id/i_must_grow_in_height"
              android:layout_width="48dp"
              android:layout_height="match_parent">
              <TextView
                  android:text="Grow and to center!!!!"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />
          </RelativeLayout>
    
    
    
          <RelativeLayout
    
              android:background="@android:color/holo_green_dark"
              android:layout_toRightOf="@+id/i_must_grow_in_height"
    
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_toLeftOf="@+id/relativeLayout4">
    
              <LinearLayout
                  android:gravity="center_vertical"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
    
                  <TextView
                      android:text="Im the content i can grow in height but the side panels must GROW TOO"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content" />
    
                  <TextView
                      android:text="Im the content i can grow in height but the side panels must GROW TOO"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content" />
    
                  <TextView
                      android:text="Im the content i can grow in height but the side panels must GROW TOO"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content" />
    
                  <TextView
                      android:text="Im the content i can grow in height but the side panels must GROW TOO"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content" />
    
                  <Button
                      android:text="Random Content"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content" />
                  <Button
                      android:text="Random Content"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content" />
                  <Button
                      android:text="Random Content"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content" />
                  <Button
                      android:text="Random Content"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content" />
                  <Button
                      android:text="Random Content"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content" />
                  <Button
                      android:text="Random Content"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content" />
    
    
              </LinearLayout>
          </RelativeLayout>
    
    
    
          <RelativeLayout
              android:background="@android:color/holo_red_dark"
              android:layout_width="48dp"
              android:gravity="center_vertical"
    
              android:layout_alignParentRight="true"
              android:layout_height="match_parent"
              android:id="@+id/relativeLayout4">
    
              <TextView
                  android:text="C'mon i need it too"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />
    
          </RelativeLayout>
    
      </RelativeLayout>
    </RelativeLayout>
    

    有人可以解释为什么它没有增长(红色布局)?

2 个答案:

答案 0 :(得分:2)

在您使用过

的布局中

android:background="@android:color/holo_red_dark"

根据内容而不是容器的高度自动调整高度。

android:layout_height="wrap_content"

如果你想让图像按钮从头开始变红,那么尝试将红色RelativeLayout中的textView高度作为match_parent。

<RelativeLayout
        android:id="@+id/relativeLayout4"
        android:layout_width="48dp"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:background="@color/Red"
        android:gravity="center" >

        <TextView
            android:layout_width="wrap_content"
            android:gravity="center"
            android:layout_height="match_parent"
            android:text="C&apos;mon i need it too" />
</RelativeLayout>

我的LG OG(4.1.2)的屏幕截图。

enter image description here   这是完整的XMl,非常微小的变化,我认为你应该清理项目并尝试其他设备或模拟器......

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RelativeLayout
    android:id="@+id/thebig_father_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/Blue" >

    <RelativeLayout
        android:id="@+id/i_must_grow_in_height"
        android:layout_width="48dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@color/Red"
        android:gravity="center_vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Grow and to center!!!!" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toEndOf="@+id/i_must_grow_in_height"
        android:layout_toLeftOf="@+id/relativeLayout4"
        android:layout_toRightOf="@+id/i_must_grow_in_height"
        android:layout_toStartOf="@+id/relativeLayout4"
        android:background="@color/Blue" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Im the content i can grow in height but the side panels must GROW TOO" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Im the content i can grow in height but the side panels must GROW TOO" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Im the content i can grow in height but the side panels must GROW TOO" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Im the content i can grow in height but the side panels must GROW TOO" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Random Content" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Random Content" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Random Content" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Random Content" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Random Content" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Random Content" />
        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout4"
        android:layout_width="48dp"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:background="@color/Red"
        android:gravity="center" >

        <TextView
            android:layout_width="wrap_content"
            android:gravity="center"
            android:layout_height="match_parent"
            android:text="C&apos;mon i need it too" />
    </RelativeLayout>
</RelativeLayout>

答案 1 :(得分:1)

您发布的 row.xml 代码对我有用吗?检查项目中是否有多个不同密度的row.xml文件等(包括模块/库)。轻松检查此方法的一种方法是将指针悬停在Java代码中的 R.layout.row 上方,然后按住Ctrl键单击。这将给出存在此row.xml文件的所有位置。

查看v = LayoutInflater.from(parent.getContext())。inflate( R.layout.row ,parent,false);

我建议使用较少的嵌套视图,这也可以使用LinearLayout来完成:

<强> RelativeLayout的

<?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="match_parent"
android:background="@android:color/holo_blue_bright">

<ImageButton
    android:id="@+id/imageButtonOnLeft"
    android:layout_width="48dp"
    android:layout_height="match_parent"

    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"

    android:layout_gravity="center_vertical"
    android:background="@android:color/holo_red_dark"
    android:contentDescription="info button"
    android:src="@android:drawable/ic_dialog_info"
    android:text="Grow and to center!!!!" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"

    android:layout_toEndOf="@+id/imageButtonOnLeft"
    android:layout_toRightOf="@+id/imageButtonOnLeft"

    android:layout_toLeftOf="@+id/imageButtonOnRight"
    android:layout_toStartOf="@+id/imageButtonOnRight"

    android:background="@android:color/holo_green_dark"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:padding="8dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Im the content i can grow in height but the side panels must GROW TOO" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Random Content" />

</LinearLayout>

<ImageButton
    android:id="@+id/imageButtonOnRight"
    android:layout_width="48dp"
    android:layout_height="match_parent"

    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"

    android:layout_gravity="center_vertical"
    android:background="@android:color/holo_red_dark"
    android:contentDescription="dialer button"
    android:src="@android:drawable/ic_dialog_dialer"
    android:text="C'mon i need it too" />

</RelativeLayout>

<强>的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="match_parent"
android:background="@android:color/holo_blue_bright"
android:orientation="horizontal">


<ImageButton
    android:id="@+id/imageButtonOnLeft"
    android:layout_width="48dp"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:background="@android:color/holo_red_dark"
    android:contentDescription="info button"
    android:src="@android:drawable/ic_dialog_info"
    android:text="Grow and to center!!!!" />


<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:background="@android:color/holo_green_dark"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:padding="8dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Im the content i can grow in height but the side panels must GROW TOO" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Random Content" />


</LinearLayout>


<ImageButton
    android:id="@+id/imageButtonOnRight"
    android:layout_width="48dp"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:background="@android:color/holo_red_dark"
    android:contentDescription="dialer button"
    android:src="@android:drawable/ic_dialog_dialer"
    android:text="C'mon i need it too" />


 </LinearLayout>