简体   繁体   中英

Android Studio - How to programmatically add layout_below param to CardViews inside a PercentRelativeLayout

I am here because I am trying to set programmatically the layout_below parameter of some cardViews without success. These cardViews are located inside a PercentRelativeLayout (I do not know if it is relevant but these cardviews have been added programmatically too). I show you the code to make it clear.

                          ...

<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>

                                     ...

I tried to do something like this in my Java code but without success.

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

As you can see, I am trying to generate cardviews and insert them one below the other.

Thanks in advance for your help.

PercentRelativeLayout is depreciated, so you are encouraged to use other type of layout.

This class was deprecated in API level 26.1.0. consider using ConstraintLayout and associated layouts instead. The following shows how to replicate the functionality of percentage layouts with a ConstraintLayout. The Guidelines are used to define each percentage break point, and then a Button view is stretched to fill the gap:

ConstraintLayout gives lots of opportunities. More about PercentRelativeLayout deprecation here . More about ConstraintLayout and how to use it here or video tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM