PercentRelativeLayout,如何以编程方式设置高度

时间:2015-08-30 02:40:49

标签: java android android-relativelayout android-percent-library

我正在使用Android百分比支持包中的PercentRelativeLayout。这就是我的布局。

<android.support.percent.PercentRelativeLayout
    android:id="@+id/view_parent"
    app:layout_heightPercent="68%" android:visibility="invisible"
    android:layout_width="match_parent"
    android:background="@color/mountain_meadow" android:layout_alignParentTop="true"
    android:layout_height="wrap_content">


    <View android:id="@+id/view_child" android:layout_width="match_parent" app:layout_heightPercent="30%"
        android:layout_alignParentBottom="true"
        />

</android.support.percent.PercentRelativeLayout>

我想以编程方式更改view_child的高度。如何使用PercentRelativeLayout.LayoutParams或任何其他方式执行此操作。

1 个答案:

答案 0 :(得分:67)

您想设置新的百分比值吗?如果是,您需要:

View view = findViewById(R.id.view_child);
PercentRelativeLayout.LayoutParams params = (PercentRelativeLayout.LayoutParams) view.getLayoutParams();
// This will currently return null, if it was not constructed from XML.
PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
info.heightPercent = 0.60f;
view.requestLayout();