简体   繁体   中英

Position LinearLayout inside RelativeLayout dynamically

I'm trying to position a LinearLayout below another element inside a RelativeLayout .

This is my xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_android"
    android:weightSum="1"
    android:id="@+id/family_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:id="@+id/relativeLayout">

        <ImageView
            android:layout_width="180dp"
            android:layout_height="62dp"
            android:id="@+id/imageView2"
            android:src="@drawable/test_logo"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <ImageButton
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/menu_btn"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:background="@drawable/menu_icon" />

    </RelativeLayout>


</RelativeLayout>

And I'm creating a new LinearLayout dynamically thats is working fine. The problem is that I can't seem to place the newly created element below an existing element inside the RelativeLayout .

Basically, I'm creating a new LinearLayout and I want to position it below the RelativeLayout with id relativeLayout . This is the code I have so far.

// GET ACTIVITY PARENT
RelativeLayout parent = (RelativeLayout) findViewById(R.id.family_parent);

RelativeLayout.LayoutParams parentParams = (RelativeLayout.LayoutParams) parent.getLayoutParams();

parentParams.addRule(RelativeLayout.BELOW, R.id.relativeLayout);

// CREATE CHILD LAYOUTS
LinearLayout newLLayout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams LLayoutParams = new LinearLayout.LayoutParams(
        100, ViewGroup.LayoutParams.MATCH_PARENT);

newLLayout.setLayoutParams(parentParams);

newLLayout.setBackgroundColor(Color.parseColor("#FF0000"));

parent.addView(newLLayout, LLayoutParams);

UPDATE

// GET ACTIVITY PARENT
RelativeLayout parent = (RelativeLayout) findViewById(R.id.family_parent);
RelativeLayout.LayoutParams parentParams = (RelativeLayout.LayoutParams) parent.getLayoutParams();


LinearLayout newLLayout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams LLayoutParams = new LinearLayout.LayoutParams(
        100, ViewGroup.LayoutParams.MATCH_PARENT);

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
parentParams.addRule(RelativeLayout.BELOW, R.id.relativeLayout);

newLLayout.setLayoutParams(layoutParams);

parent.addView(newLLayout, LLayoutParams);

It's giving this error:

03-17 05:12:39.831    5477-5477/pt.test_pro_gold E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{pt.test_pro_gold/pt.test_pro_gold.ui.activities.PageFamily}: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
            at pt.test_pro_gold.ui.activities.PageFamily.onCreate(PageFamily.java:46)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

Set the id to the LinearLayout

Make use of the id while adding rule

params.addRule(RelativeLayout.BELOW, linearLayout.getId());

I think you're confused with LayoutParams . When you are setting LayoutParams to a view you have to use the parent ViewGroups LayoutParams.

LinearLayout newLLayout = new LinearLayout(getApplicationContext());

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
parentParams.addRule(RelativeLayout.BELOW, R.id.relativeLayout);

newLLayout.setLayoutParams(layoutParams);

That should do it.

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