Android相对而言wrap_content和match_parent [英] Android relatively out wrap_content and match_parent
问题描述
我有一个RelativeLayout问题.它是列表项的自定义视图.我有一个RelativeLayout,它的高度设置为wrap_content.它的内部是三个线性布局,其中之一的高度已设置为wrap_content.其他两个被设置为match_parent,因为当它增长到容纳wrap_content之一时,我需要它们来填充父对象.应该增长的两个位于另一个后面(用于滑动,滑动后的顶视图不显示底部的两个).问题在于,设置为wrap_content的LinearLayout会根据需要增长. RelativeLayout根据需要增长,两个LinearLayouts没有增长.关于如何完成此操作的任何想法?
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?
更新:这是XML ...
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>
目标是使TopContentView覆盖其他两个(Left和RightContentView). TopContentView需要增长,因为其中有三个文本框,其中一个可能被隐藏,使整体布局更短.因此,左侧和右侧需要增加和缩小以匹配TopContentView.
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.
推荐答案
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"
这将它们拉伸到与父对象的TopContentView的顶部和底部对齐.我尝试使用layout_AlignParentTop ="true"和layout_AlignParentBottom ='true,但是通过将底部设置为其他效果很好的控件,将其移至底部.
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.
此外,我将LinearLayouts更改为RelativeLayouts.生成的AXML是...
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>
这篇关于Android相对而言wrap_content和match_parent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!