PercentRelativeLayout, how to set the height programmatically

元气小坏坏 提交于 2019-11-28 23:50:17

问题


I am using PercentRelativeLayout from android percent support package. this is what I have in my layout.

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

I want to change the height of view_child programmatically. How can I do that using PercentRelativeLayout.LayoutParams or any other way.


回答1:


Do you want to set a new percentage value? If yes, you need to:

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();


来源:https://stackoverflow.com/questions/32292656/percentrelativelayout-how-to-set-the-height-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!