Android在配置子视图时使用了错误的父视图';s高度设置为与父项匹配
在下面的代码中,当我将第一个子项Android在配置子视图时使用了错误的父视图';s高度设置为与父项匹配,android,android-layout,Android,Android Layout,在下面的代码中,当我将第一个子项LinearLayout的高度设置为match\u parent时,高度将根据屏幕大小放大。我不知道为什么会这样。父对象是RelativeLayout,因此当其高度设置为match\u parent时,第一个子对象LinearLayout的高度不应该与RelativeLayout的高度匹配吗 <RelativeLayout android:layout_width="match_parent" android:layout_height="w
LinearLayout
的高度设置为match\u parent
时,高度将根据屏幕大小放大。我不知道为什么会这样。父对象是RelativeLayout
,因此当其高度设置为match\u parent
时,第一个子对象LinearLayout
的高度不应该与RelativeLayout
的高度匹配吗
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@null">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_toStartOf="@+id/vote_container"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:text="1M"/>
</LinearLayout>
<LinearLayout
android:id="@id/vote_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal">
<ImageButton
android:id="@+id/upvote_button"
android:layout_width="30dp"
android:layout_height="17dp"
android:layout_centerHorizontal="true"
android:background="@null"
android:src="@drawable/ic_keyboard_arrow_up"/>
<TextView
android:id="@+id/number_of_votes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/upvote_button"
android:hint="100"/>
<ImageButton
android:id="@+id/downvote_button"
android:layout_width="30dp"
android:layout_height="17dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/number_of_votes"
android:background="@null"
android:src="@drawable/ic_keyboard_arrow_down"/>
</LinearLayout>
</RelativeLayout>
这不一定是问题的解决方案,但更多的是一种解决方法。我只是将第一个LinearLayout
的顶部和底部与第二个LinearLayout
的--@id/vote\u容器对齐。假设第一个LinearLayout
的高度永远不需要大于第二个LinearLayout
的高度,这应该不是问题。如果是的话,我可以将两者之间的约束关系颠倒过来
所述代码不起作用的原因是,RelativeLayout
的高度设置为wrap\u content
,而第一个LinearLayout
的高度设置为match\u parent
,因此,为了计算父级的高度,子级的高度是确定的,但是为了计算第一个子级的高度,父级的高度是确定的,即创建递归关系,并且Android
不能直观地处理它
在这种情况下,将搜索父级的层次结构,直到找到一个高度不是wrap\u content
的父级,并且该父级的值将用于第一个LinearLayout
的高度。在我的例子中,RelativeLayout
父对象有另一个父对象,这是一个高度也设置为wrap\u content
的LinearLayout
,因此唯一具有高度值而不是wrap\u content
的父对象是超级视图——所有其他视图都放置在其中的视图;而视图的高度只是屏幕的大小
这就是我的线性布局的高度被放大到屏幕大小的原因。这不一定是问题的解决方案,但更多的是解决方法。我只是将第一个LinearLayout
的顶部和底部与第二个LinearLayout
的--@id/vote\u容器对齐。假设第一个LinearLayout
的高度永远不需要大于第二个LinearLayout
的高度,这应该不是问题。如果是的话,我可以将两者之间的约束关系颠倒过来
所述代码不起作用的原因是,RelativeLayout
的高度设置为wrap\u content
,而第一个LinearLayout
的高度设置为match\u parent
,因此,为了计算父级的高度,子级的高度是确定的,但是为了计算第一个子级的高度,父级的高度是确定的,即创建递归关系,并且Android
不能直观地处理它
在这种情况下,将搜索父级的层次结构,直到找到一个高度不是wrap\u content
的父级,并且该父级的值将用于第一个LinearLayout
的高度。在我的例子中,RelativeLayout
父对象有另一个父对象,这是一个高度也设置为wrap\u content
的LinearLayout
,因此唯一具有高度值而不是wrap\u content
的父对象是超级视图——所有其他视图都放置在其中的视图;而视图的高度只是屏幕的大小
这就是我的线性布局的高度被放大到屏幕大小的原因。如果我想让relativelayout的高度匹配父对象,那么这就是为什么如果我想让relativelayout的高度匹配父对象,那么这将有助于解决涉及带有嵌套线性布局(水平)和两个线性布局(垂直)的TableRow。我取出了水平线性布局,因为TableRow已经是水平线性布局,并且它找到了正确的父对象。这有助于解决一个类似的问题,即具有嵌套线性布局(水平)和两个线性布局(垂直)的TableRow。我取出了水平线性布局,因为TableRow已经是水平线性布局,它找到了正确的父对象。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"