Android Studio - 如何以编程方式将layout_below参数添加到PercentRelativeLayout

时间:2018-04-07 07:28:13

标签: java android xml layout programmatically

我在这里是因为我试图以编程方式设置某些cardView的 layout_below 参数而没有成功。 这些cardViews位于PercentRelativeLayout内(我不知道它是否相关,但这些卡片视图也是以编程方式添加的)。 我告诉你代码清楚。

                          ...

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

    <android.support.v7.widget.CardView
        xmlns:percent="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cv0"
        android:layout_width="match_parent"
        percent:layout_heightPercent="65%"
        percent:layout_marginTopPercent="2%"
        percent:layout_marginLeftPercent="2%"
        percent:layout_marginRightPercent="2%"
        card_view:cardCornerRadius="2dp"
        card_view:cardElevation="4dp">
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        xmlns:percent="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cv1"

        // here I would like to add android:layout_below="@+id/cv0"

        android:layout_width="match_parent"
        percent:layout_heightPercent="65%"
        percent:layout_marginTopPercent="2%"
        percent:layout_marginLeftPercent="2%"
        percent:layout_marginRightPercent="2%"
        card_view:cardCornerRadius="2dp"
        card_view:cardElevation="4dp">
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        xmlns:percent="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cv2"

        // here I would like to add android:layout_below="@+id/cv1"

        android:layout_width="match_parent"
        percent:layout_heightPercent="65%"
        percent:layout_marginTopPercent="2%"
        percent:layout_marginLeftPercent="2%"
        percent:layout_marginRightPercent="2%"
        card_view:cardCornerRadius="2dp"
        card_view:cardElevation="4dp">
    </android.support.v7.widget.CardView>


</android.support.percent.PercentRelativeLayout>

                                     ...

我尝试在我的Java代码中执行类似的操作,但没有成功。

PercentRelativeLayout prl = (PercentRelativeLayout) view.findViewById(R.id.prl);
CardView cv = (CardView) LayoutInflater.from(getActivity()).inflate(R.layout.cv_block, prl, false);
cv.setId(currentId++);
prl.addView(cv);

cv = (CardView) LayoutInflater.from(getActivity()).inflate(R.layout.cv_block, prl, false);
cv.setId(currentId++);

RelativeLayout.LayoutParams cv_lp = (RelativeLayout.LayoutParams) cv.getLayoutParams();
cv_lp.addRule(RelativeLayout.BELOW,currentId-2);

/* I have also tried to call cv.requestLayout() but nothing changed */

prl.addView(cv);

正如您所看到的,我正在尝试生成卡片视图并将它们插入另一个之下。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

PercentRelativeLayout已折旧,因此建议您使用其他类型的布局。

  

此类在API级别26.1.0中已弃用。   考虑使用ConstraintLayout和相关的布局。以下显示如何使用ConstraintLayout复制百分比布局的功能。指南用于定义每个百分比断点,然后拉伸按钮视图以填补空白:

ConstraintLayout提供了很多机会。 有关PercentRelativeLayout弃用的更多信息here。 有关ConstraintLayout的更多信息以及如何使用它herevideo tutorial