简体   繁体   English

如何在不影响父视图的情况下设置子视图的Layoutparams?

[英]How to set Layoutparams of child view without affecting parent view?

I have a custom row view for a list. 我有一个列表的自定义行视图。 Say there are 2 components a parent and a child....i want to maintain the height of the parent to be constant when i change the height of the child... 说父母和孩子有2个组成部分....当我改变孩子的身高时,我想保持父母的身高恒定。

I have set the parent height constant and the height of the child is varied dynamically. 我已经设置了父母的身高常数,孩子的身高是动态变化的。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:background="#fff"
android:orientation="vertical" >


<RelativeLayout
    android:id="@+id/child"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon" />

</RelativeLayout>
</LinearLayout>

and during runtime, 在运行时

child.setLayoutParams(new LayoutParams(30, x);

where x is a varying height variable. 其中x是变化的高度变量。 Still the height of the parent is getting affected. 父母的身高仍然受到影响。 any solutions? 有什么解决办法吗?

If the LinearLayout is the root view, when you are inflating it the params android:layout_width and android:layout_height are changed by android, if it is a row inside a list, then they will have the width match_parent and the height wrap_content. 如果LinearLayout是根视图,则在对其进行充气时,android将更改android:layout_width和android:layout_height的参数,如果它在列表中的一行,则它们的宽度将为match_parent,高度为wrap_content。 If you want to keep a constant height for the row you will have to set the layout params programatically after you inflate the view. 如果要使行的高度保持恒定,则必须在给视图充气后以编程方式设置布局参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM