在Java中设置PercentRelativeLayout百分比

时间:2016-03-10 11:13:28

标签: java android android-layout android-percentrelativelayout

我想基于某些事件动态地在Java中更改PercentRelativeLayout内的项目的宽度百分比,我发现了这一点,但我找不到将百分比放在哪里:

mLinerLayout.setLayoutParams(new PercentRelativeLayout.LayoutParams());

我的xml看起来像这样:

<android.support.percent.PercentRelativeLayout
    android:id="@+id/ad_prl_tags"
    android:layout_width="match_parent"
    android:layout_height="48dp">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/ad_rv_tags"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        app:layout_widthPercent="70%" />

    <!--Add Tags Button-->

    <LinearLayout
        android:id="@+id/ad_ll_add_tags_container"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/ad_rv_tags">

        <ImageView
            android:id="@+id/itd_animatable_plus_iv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />

        <EditText
            android:id="@+id/ad_et_add_tags_and_info"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </LinearLayout>
</android.support.percent.PercentRelativeLayout>

我想在Java中将LinearLayout的宽度百分比设置为70%,将RecyclerView的宽度设置为30%。

1 个答案:

答案 0 :(得分:4)

您可以使用此方法实用地使用它:

public static void setWidthPercent(View view, float width) {
    PercentLayoutHelper.PercentLayoutParams params =(PercentLayoutHelper.PercentLayoutParams) view.getLayoutParams();
    PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
    info.widthPercent = width;
}