2013-08-26 101 views
6

嗨我已经创建了一个包含2个按钮,一个单选按钮和一个图形视图的RelativeLayout。将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> 
+0

我没有在这里得到问题吗?你到底想做什么?你是否想要在屏幕上添加两个图表,都占用相同的空间?或者您可以上传显示所需布局的样本图纸。 – Sagar

+0

是的,我现在的布局是一个相对布局,图形视图显示波形的ECG信号。我现在想把这张图分成两半,另一半显示呼吸信号。底部有三个按钮,应该放在同一个地方 –

回答

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> 
+0

感谢类似的东西。但第一个卢布是这两个图形视图像垂直,但我需要水平像屏幕分为两个。而且这两张图看起来都完全相同。有像第一个图上的值,但我想在画布上添加其他内容。例如:我的Graphview由画布上的HR和温度组成,现在我想将呼吸细节粘贴到画布上。 –

+0

@RahulSharma:可以绘制所需布局的简单草图并上传?它会更容易理解。 – Sagar

+0

我知道@sagar,但不幸的是我没有足够的声望点来粘贴screenhot,因为我是新的.......但让我试着在文本中显示一些东西----------- ----------------------------------------------- graphview1:温度-------------------------------------------------- ------------- graphview2:呼吸---------------------------------- ---------------------------- 3按钮 –

0

这应该工作。我删除RelativeLayoutLinearLayout为使用layout_weight

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight=".50" > 

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

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

     <RadioButton 
      android:id="@+id/BtnSaveFile" 
      android:layout_width="100dip" 
      android:layout_height="wrap_content" 
      android:text="SaveFile" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight=".50" > 

     <helog.diwesh.NugaBest.GraphView 
      android:id="@+id/gview" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 

</LinearLayout> 
+0

感谢您的及时回复@叶琳昂....但是当我粘贴代码并在图形布局中看到时。我看不到变化。 –

+0

对不起。我已经纠正了答案。 –

+0

嗨,谢谢我收到以下错误:根元素后面的文档中的标记必须格式良好。再次修复 –

14

所以,对于这一点,最好的办法我发现(这感觉就像一个黑客总额,但就像一个魅力)就是具有零尺寸显示对齐布局的中心,然后将左半部分与该视图的左侧对齐,将该右侧的右半部分与该视图对齐。例如:

<?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> 
+0

嗨,谢谢我试过这个,但它给了我以下错误:错误:错误:字符串类型不允许('layout_toRightOf'值'锚')。 –

+1

固定。只是忘记了ID,但这是你应该能够检查的常见问题。 – kcoppock

+0

正是我想要的! :) –

5

使用的LinearLayout,而不是RelativeLayout.LinearLayout让你想要使用的LinearLayout的layout_weight财产你把你的屏幕在准确二段垂直或水平。

使用layout_weight可以指定多个视图之间的大小比例。例如。你有一个MapView和一个表格,它应该向地图显示一些额外的信息。地图应该使用3/4的屏幕,桌子应该使用1/4的屏幕。然后,将地图的layout_weight设置为3,将table的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> 

在代码安卓方向是,如果你使用垂直指定orienttation那么它会垂直arrage它的孩子,如果你指定水平那么它将水平排列其子。

+0

@kcoppock解决方案更好的情况下,如果需要列表性能。 – CoDe