简体   繁体   中英

How can I add two Scroll views in a xml layout such that each scrollview take half of the height of the layout?

How do I add two ScrollViews in an android XML-layout such that each scroll view takes half of the height of the layout?

在此处输入图片说明

You can use LinearLayout as rootview then add two ScrollView as child and assign android:layout_weight="1" to both ScrollView

Note : if you want your view scroll horizontally then use HorizontalScrollView

SAMPLE CODE

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@android:color/black"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <!--add viw here-->
        </LinearLayout>

    </ScrollView>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@android:color/holo_red_dark"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <!--add viw here-->
        </LinearLayout>

    </ScrollView>


</LinearLayout>

OUTPUT

在此处输入图片说明

There is a multiple way to do this. I am suggesting you one simple way. try to add weightsum =2 inside parent layout. And devide layout with 1 like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_rel"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="2.0" >

    <RelativeLayout
        android:id="@+id/child_one"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:background="#0000FF" >
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/child_two"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:background="#00FF00" >
    </RelativeLayout>

</LinearLayout>

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