将RelativeLayout分成两半

Rah*_*rma 6 android android-layout

嗨我创建了一个包含2个按钮,一个单选按钮和一个图表视图的RelativeLayout.

我想现在在图表中显示两个不同的数据.

如何将RelativeLayout分成两半?

这是我目前的XML代码:

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

    <Button
        android:id="@+id/BtnStart"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="5dip"
        android:text="SlaveEnable" />

    <Button
        android:id="@+id/BtnStop"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="5dip"
        android:layout_toRightOf="@id/BtnStart"
        android:text="SlaveDisable" />

    <RadioButton
        android:id="@+id/BtnSaveFile"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@id/BtnStop"
        android:text="SaveFile" />

    <helog.diwesh.NugaBest.GraphView
        android:id="@+id/gview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/BtnStart"
        android:layout_alignParentRight="true" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

kco*_*ock 17

所以,对于这个,我发现的最好的方式(感觉就像一个完全黑客,但工作就像一个魅力)是将零大小的视图与布局的中心对齐,然后将左半部分对齐到左边该视图的右半部分与右视图对齐.例如:

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

    <View
        android:id="@+id/anchor"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent="true" />

    <Button
        android:id="@+id/left_side"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/anchor"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="5dip"
        android:text="Left Side" />

    <Button
        android:id="@+id/right_side"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/anchor"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="5dip"
        android:text="Right Side" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)


Jit*_*iya 5

使用Linearlayout而不是RelativeLayout.LinearLayout,您可以使用LinearLayout的layout_weight属性,根据需要垂直或水平地将屏幕划分为两个部分.

使用layout_weight,您可以指定多个视图之间的大小比率.例如,你有一个MapView和一个表格,它应该向地图显示一些额外的信息.地图应使用屏幕的3/4,表格应使用屏幕的1/4.然后,您将地图的layout_weight设置为3,将表的layout_weight设置为1.

编写如下代码,将屏幕分为两部分.

<LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:weightSum="2"
        android:orientation="vertical">
    <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_weight="1">
    </LinearLayout>
    <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_weight="1">
    <LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在代码android:orientation是指定方向,如果你使用垂直,那么它将垂直arcing它的子,如果你指定水平,那么它将水平排列其子.


Sag*_*gar 2

试试这个布局。如果不完美的话,应该可以进行一些更改。这将添加两个图表。

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

    <Button
        android:id="@+id/BtnStart"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="5dip"
        android:text="SlaveEnable" />

    <Button
        android:id="@+id/BtnStop"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="5dip"
        android:layout_toRightOf="@id/BtnStart"
        android:text="SlaveDisable" />

    <RadioButton
        android:id="@+id/BtnSaveFile"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@id/BtnStop"
        android:text="SaveFile" />

<LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
            android:layout_above="@id/BtnStart"
            android:layout_alignParentRight="true"
        android:orientation="vertical" 
    android:weightSum="2" >

    <helog.diwesh.NugaBest.GraphView
                android:id="@+id/gview1"
                android:layout_height="0dp"
                android:layout_width="match_parent"
        android:layout_weight="1" />

    <helog.diwesh.NugaBest.GraphView
                android:id="@+id/gview2"
                android:layout_height="0dp"
                android:layout_width="match_parent"
        android:layout_weight="1" />

</LinearLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)