开发者

Layouts on top of each other

开发者 https://www.devze.com 2023-01-16 19:42 出处:网络
This is an android development question. If I first set the content view to my xml layout using se开发者_开发问答tContentView(R.layout.main); and then add another content view using addContentView(lay

This is an android development question.

If I first set the content view to my xml layout using se开发者_开发问答tContentView(R.layout.main); and then add another content view using addContentView(layout, params), the content views overlap each other. I want the second content view to position itself directly under the first one. Is it possible or do I need to combine the xml and the programatically created layouts in some way? Both of the layouts are linear.

Thanks in advance


Put both Layouts inside an LinearLayout with vertical orientation. thats it.

Edit:

what i mean is you have to create a 3 layout xml files.

  1. Main.xml
  2. layout1.xml --> your first layout
  3. layout2.xml --> your second layout

your main layout file shoul be like this:

<LinearLayout android:orientation="vertical">
<include android:id="+id/lay1" android:layout="@layout/layout1"/>
<include android:id="+id/lay2" android:layout="@layout/layout2"/>
</LinearLayout>

Now your the main layout to your setContentView(R.layout.main)


Thanks guys. I appreciate the feedback.

It didn't work to make the orientation vertical on the parent layout (CoordinatorLayout).

What worked though was to put both elements in a LinearLayout with vertical orientation.

One comment above said I should make first layout an appbarlayout: Yes, I changed that back. I had changed it to a LinearLayout as one of my attempts to get the items to stop painting on top of each other.

I'm not sure that was the correct way to solve this, but it worked. I'll move on. Thanks again.


To place a layout on top of each other (one top the other under), this is what I did:

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent">


<RelativeLayout
    android:id="@+id/topLayout"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentTop="true"
    android:background="@color/colorAccent">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="126dp"
        android:text="Existing Client"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:textStyle="bold" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@color/colorPrimary">

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="138dp"
        android:text="New Client"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:textStyle="bold" />

</RelativeLayout>

And the result:

Layouts on top of each other

My intention was to use the layouts as buttons that fill a certain width of the screen.

0

精彩评论

暂无评论...
验证码 换一张
取 消