Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中设置PercentRelativeLayout百分比_Java_Android_Android Layout_Android Percentrelativelayout - Fatal编程技术网

在Java中设置PercentRelativeLayout百分比

在Java中设置PercentRelativeLayout百分比,java,android,android-layout,android-percentrelativelayout,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

我想根据一些事件在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%。

您可以通过以下方法实际使用它:

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

我对此也很好奇,所以我查看了百分比布局代码。事实证明,它只是为从布局XML文件中获取百分比而设置的。与其弄清楚如何像从XML解析属性集那样以编程方式创建属性集,不如为包装器创建一个自定义LinearLayout类,并重写onLayout来自己进行百分比计算。