【问题标题】:How to set Layoutparams of child view without affecting parent view?如何在不影响父视图的情况下设置子视图的Layoutparams?
【发布时间】:2013-05-08 18:30:56
【问题描述】:

我有一个列表的自定义行视图。假设有 2 个组件,一个父项和一个子项....当我更改子项的高度时,我想保持父项的高度不变...

我已经设置了父高度不变,孩子的高度是动态变化的。

<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>

在运行时,

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

其中 x 是可变高度变量。 父母的身高仍然受到影响。有什么解决办法吗?

【问题讨论】:

  • 您能否提供更多细节:1)还有其他孩子(兄弟姐妹)吗? 2) linearlayout 父级是布局的父级,还是嵌入在另一个布局中(如果是,该父级的 layout_height 设置是什么)? 3) 你是否曾经将 x 设置为大于 150dp(你的父母身高)?
  • 1) 嘿,还有其他兄弟姐妹,但没有什么相互依赖 2)Linearlayout 是根布局,它没有任何父布局 3)不,我从来没有设置 x>150

标签: android android-layout layout resize


【解决方案1】:

如果 LinearLayout 是根视图,当你膨胀它时,参数 android:layout_width 和 android:layout_height 由 android 更改,如果它是列表中的一行,那么它们将具有宽度 match_parent 和高度 wrap_content .如果您想为该行保持恒定的高度,则必须在膨胀视图后以编程方式设置布局参数。

【讨论】:

    猜你喜欢
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 2019-03-25
    • 2020-09-04
    • 1970-01-01
    • 2013-03-24
    • 2011-06-27
    相关资源
    最近更新 更多