Android relatively out wrap_content and match_parent

梦想与她 提交于 2020-01-05 03:56:06

问题


I have an issue with a RelativeLayout. It is a custom view for a List Item. I have a RelativeLayout, it's height is set to wrap_content. Inside of it are three Linear Layouts, one of which has it's height set to wrap_content. The other two are set to match_parent as I need them to fill the parent after it has grown to accommodate the wrap_content one. The two that are supposed to grow are behind the other one (it's used for swiping, The top view when swiped moves out of the way to reveal the bottom two). The ptoblem is, the LinearLayout that is set to wrap_content grows as needed. The RelativeLayout grows as needed, the two LinearLayouts are not growing. Any ideas of how to accomplish this?

Update: Here is the XML...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="fill_vertical"
    android:background="@color/black"
    android:id="@+id/Swipe"
  >
  <LinearLayout
    android:id="@+id/LeftContentView"
    android:layout_width="175dp"
    android:layout_height="match_parent"
    android:background="@color/yellow"
    android:layout_alignParentLeft="true"
    android:orientation="horizontal"
    >
    <Button 
      android:id="@+id/ApproveButton"
      android:layout_width="0dp"
      android:layout_weight=".72"
      android:layout_height="match_parent"
      android:background="#2796C3"
      android:text="Approve"
      android:layout_alignParentLeft="true"
      />
      <Button 
      android:id="@+id/ApproveUndoButton"
      android:layout_width="0dp"
      android:layout_weight=".28"
      android:layout_height="match_parent"
      android:background="#215681"
      android:text="Undo"
      android:layout_toRightOf="@id/ApproveButton"
      />
  </LinearLayout>

  <LinearLayout
  android:layout_alignParentRight="true"
  android:id="@+id/RightContentView"
  android:layout_width="175dp"
  android:layout_height="match_parent"
  android:background="@color/black"
  android:orientation="horizontal"
    >
    <Button
      android:id="@+id/DenyButton"
      android:layout_width="0dp"
      android:layout_weight=".72"
      android:layout_height="match_parent"
      android:background="#FF0000"
      android:text="Deny"
      />
    <Button
      android:id="@+id/DenyUndoButton"
      android:layout_width="0dp"
      android:layout_weight=".28"
      android:layout_height="match_parent"
      android:background="#860000"
      android:text="Undo"
      />
  </LinearLayout>

  <LinearLayout
    android:id="@+id/TopContentView"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#1F1F1F">
    <LinearLayout
      android:layout_height="fill_parent"
      android:layout_width="match_parent" >
      <ImageView 
        android:id="@+id/UnreadImage"
        android:layout_height="match_parent" 
        android:layout_width="7dp" 
        android:src="@drawable/vertical_blue_bar"
        android:background="#2796C3"/>  
      <LinearLayout
        android:id="@+id/ListText"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dip"
        android:padding="12dp">
        <TextView
             android:id="@+id/Text_Line1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:textColor="@color/white"
             android:textSize="13dip"
             />
        <TextView
               android:id="@+id/Text_Line2"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:textColor="@color/white"
               android:textSize="13dip"
             />
        <TextView
             android:id="@+id/Text_Line3"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:textColor="@color/white"
             android:textSize="11dip"
             />
      </LinearLayout>
    </LinearLayout>
  </LinearLayout>
</RelativeLayout>

The goal is for TopContentView to cover the other two (Left and RightContentView). TopContentView needs to grow as there are three text boxes within it and one of them could be hidden making the overall layout shorter. So the Left and Right need to grow and shrink to match the TopContentView.


回答1:


I finally found this answer here... How to get a button's height to match another element's height? and scrolled down to Falmarri's answer, and the answer by hcpl is essentially the same but gives more detail along with code samples. Basically I changed my LeftContentView and RightContentView linear layouts to have a height of wrap_content, then I added these two parameters...

android:layout_alignParentTop="true"
android:layout_alignBottom="@+id/TopContentView"

This stretched them out to align with the parent's top and the bottom of TopContentView. I tried using layout_AlignParentTop="true" and layout_AlignParentBottom='true" but that just moved it to the bottom, by setting the bottom to the other control it worked beautifully.

Additionally I change my LinearLayouts to be RelativeLayouts. The resulting AXML is...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="fill_vertical"
    android:background="@color/black"
    android:id="@+id/Swipe"
  >
  <RelativeLayout
    android:id="@+id/LeftContentView"
    android:layout_width="175dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@color/black"
    android:layout_alignParentLeft="true"
    android:layout_alignBottom="@+id/TopContentView"
    >
    <Button 
      android:id="@+id/ApproveButton"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#2796C3"
      android:text="Approve"
      android:layout_alignParentLeft="true"
      />
      <Button 
      android:id="@+id/ApproveUndoButton"
      android:layout_width="50dp"
      android:layout_height="match_parent"
      android:background="#215681"
      android:text="Undo"
      android:layout_alignParentRight="true"
      />
  </RelativeLayout>

  <RelativeLayout
  android:id="@+id/RightContentView"
  android:layout_width="175dp"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true"
  android:background="@color/black"
  android:layout_alignParentRight="true"
  android:layout_alignBottom="@+id/TopContentView"
    >
    <Button
      android:id="@+id/DenyButton"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#FF0000"
      android:text="Deny"
      />
    <Button
      android:id="@+id/DenyUndoButton"
      android:layout_width="50dp"
      android:layout_height="match_parent"
      android:background="#860000"
      android:layout_alignParentLeft="true"
      android:text="Undo"
      />
  </RelativeLayout>

  <LinearLayout
    android:id="@+id/TopContentView"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#1F1F1F">
    <LinearLayout
      android:layout_height="fill_parent"
      android:layout_width="match_parent" >
      <ImageView 
        android:id="@+id/UnreadImage"
        android:layout_height="match_parent" 
        android:layout_width="7dp" 
        android:src="@drawable/vertical_blue_bar"
        android:background="#2796C3"/>  
      <LinearLayout
        android:id="@+id/ListText"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dip"
        android:padding="12dp">
        <TextView
             android:id="@+id/Text_Line1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:textColor="@color/white"
             android:textSize="13dip"
             />
        <TextView
               android:id="@+id/Text_Line2"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:textColor="@color/white"
               android:textSize="13dip"
             />
        <TextView
             android:id="@+id/Text_Line3"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:textColor="@color/white"
             android:textSize="11dip"
             />
      </LinearLayout>
    </LinearLayout>
  </LinearLayout>
</RelativeLayout>


来源:https://stackoverflow.com/questions/39933768/android-relatively-out-wrap-content-and-match-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!