【问题标题】:PercentRelativeLayout, how to set the height programmaticallyPercentRelativeLayout,如何以编程方式设置高度
【发布时间】:2015-08-30 02:40:49
【问题描述】:

我正在使用来自 android percent 支持包的 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 或任何其他方式做到这一点。

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 如何设置另一个没有百分比的值?例如,dp 中的 marginBottom,使用百分比布局参数?
    • 嗨@gruszczy,你知道为什么我总是将 PercentLayoutInfo 设为 'null' 吗?请帮帮我。
    • 嗨@gruszczy,根据您的评论'//如果它不是从XML构造的,则当前将返回null。'。请帮我在 XML 中添加这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 2012-12-08
    • 1970-01-01
    • 2023-03-11
    • 2013-12-15
    相关资源
    最近更新 更多