如何将Scrollview布局分成两部分以显示父背景颜色和两个窗格? [英] How do I split a Scrollview layout in half to show parent background color and two panes?

查看:87
本文介绍了如何将Scrollview布局分成两部分以显示父背景颜色和两个窗格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有白色背景的简单UI屏幕.然后我有Scrollview,周围有10 dp的空白,黑色背景,因此基本上是一个矩形内的一个矩形.如何将Scrollview分成两半,以便可以在内部黑色矩形的中间显示一条白色水平线,从而创建两个黑色窗格?下面上传的图片显示了我目前的情况(请忽略捕获的微小外部彩色边框,以显示主UI白色背景.

I have a simple UI screen with a white background. Then I have Scrollview with 10 dp margins all around and black background, so basically a rectangle within a rectangle. 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? 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.

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>

推荐答案

我的方法是将黑色背景设置为两个矩形,而不是将其设置为无法拆分的ScrollView.此外,ScrollView只能容纳一个直子,然后向其添加符合矩形的两种布局:

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. Also a ScrollView can only host one direct child and then add to it the two layouts that conforms your rectangle:

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

请注意,我已经放置了一个Space元素来分隔两个线性布局.您可以根据需要将其替换为边距.

Note that I've put a Space element to separate the two Linear Layouts. You can replace it with a margin if you prefer.

这篇关于如何将Scrollview布局分成两部分以显示父背景颜色和两个窗格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆