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

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

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

最佳答案

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

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

关于java - PercentRelativeLayout,如何以编程方式设置高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32292656/

相关文章:

java - Tomcat 全局连接池获取错误

android - ResultCode 和 RequestCode 不工作

android - 未使用 alignParentBottom

android - RelativeLayout 或 TableLayout Android 3 按钮

android - RelativeLayout,将一个 View 对齐在另一个 View 的底部,但始终在另一个 View 下方

java - 无论屏幕方向如何,如何获得正确的方位(磁性方向)?

java - 有没有办法在构建之前告诉 Eclipse "Save all"?

c# - Unity3d Android 插件静态 AsyncTask 问题

android createTempFile 抛出权限被拒绝?

Android:光标窗口已满