简体   繁体   English

如何将Scrollview布局分成两部分以显示父背景颜色和两个窗格?

[英]How do I split a Scrollview layout in half to show parent background color and two panes?

I have a simple UI screen with a white background. 我有一个带有白色背景的简单UI屏幕。 Then I have Scrollview with 10 dp margins all around and black background, so basically a rectangle within a rectangle. 然后我有Scrollview,周围有10 dp的空白,黑色背景,因此基本上是一个矩形内的一个矩形。 How would I split the Scrollview in half so that I can show a white horizontal line in the middle of the inside black rectangle, thus creating two black panes? 如何将Scrollview分成两半,以便可以在内部黑色矩形的中间显示一条白色水平线,从而创建两个黑色窗格? The uploaded pic below shows what I have currently (please ignore the tiny outside colored border that was captured to show the main UI white background. 下面上传的图片显示了我当前的情况(请忽略捕获的微小外部彩色边框,以显示主UI白色背景。

layout.xml: layout.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
style="@style/scrollbar_shape_style"
android:focusableInTouchMode="true"
tools:context=".MainActivity" >

<ScrollView
    android:id="@+id/ScrollView2"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#000000"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"  >

<LinearLayout
    android:id="@+id/MainLayout1"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:orientation="vertical"
    android:background="#FFFFFF"
    >

</LinearLayout>

</ScrollView>

<TextView
    android:id="@+id/xyztext"
    android:text="xyz"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textStyle="bold"
    android:textColor="#FFFFFF"
    android:background="@color/colorPrimary"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:clickable="true"  />

</LinearLayout>

在此处输入图片说明

My approach would be to set the black background to the two rectangles instead of setting it to the ScrollView, which you can't split. 我的方法是将黑色背景设置为两个矩形,而不是将其设置为无法拆分的ScrollView。 Also a ScrollView can only host one direct child and then add to it the two layouts that conforms your rectangle: 另外,ScrollView只能容纳一个直子,然后向其添加符合矩形的两种布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:focusableInTouchMode="true"
    tools:context=".MainActivity" >

    <ScrollView
        android:id="@+id/ScrollView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:background="@android:color/transparent">

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

            <LinearLayout
                android:id="@+id/MainLayout1"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:orientation="vertical"
                android:background="@android:color/black">
            </LinearLayout>

            <android.support.v4.widget.Space
                android:layout_width="match_parent"
                android:layout_height="5dp" />

            <LinearLayout
                android:id="@+id/MainLayout2"
                android:layout_width="match_parent"
                android:layout_height="700dp"
                android:orientation="vertical"
                android:background="@android:color/black">

            </LinearLayout>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

This will give you the following UI: 这将为您提供以下UI:

UI结果

Note that I've put a Space element to separate the two Linear Layouts. 请注意,我已经放置了一个Space元素来分隔两个线性布局。 You can replace it with a margin if you prefer. 如果愿意,可以将其替换为边距。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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