2015-04-04 54 views
0

我怎样才能达到下一个布局位置(高度)?按版式排列的Android布局

enter image description here

当前代码:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <ListView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"></ListView> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true"> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAlignment="center" 
      android:text="Новая заметка"/> 
    </RelativeLayout> 
</RelativeLayout> 
+2

请问您是否需要清楚“layout by layout”是什么意思? – Gumbo 2015-04-04 16:24:17

+0

我想布局有高度位置,如表中的表行。我的意思是,布局高度1 +布局高度2 =活动高度(一个接一个) – FSou1 2015-04-04 16:25:42

+0

的确,“layout by layout”的含义并不完全清楚,但您的图片布局可以通过使用一个父“LinearLayout”方向和两个孩子('ListView'和'Button')与指定的权重。 – AndroidEx 2015-04-04 16:27:20

回答

1

我建议这个

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/myButton"> 

    </ListView> 

    <Button 
     android:id="@+id/myButton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="22dp" 
     android:text="New Note" 
     android:layout_marginBottom="5dp" 
     android:layout_alignParentBottom="true" /> 

</RelativeLayout> 

因为我没有看到把相对布局insid另一个相对布局的原因。

+0

看起来像我只需要'android:layout_above'来达到我的目标 – FSou1 2015-04-04 16:44:06

+0

仍然,'RelativeLayout'是基本垂直布局的浪费:)它是为了减少复杂布局中的层次数量(LinearLayout),因为两个层次的LinearLayout被认为比RelativeLayout的一个层次差,但显然是一个层次'RelativeLayout'的布局比'LinearLayout'的一个层次更重要。 – AndroidEx 2015-04-04 16:56:50

2

可以达到相同的布局时,您将添加 “layout_above” 属性中使用此代码。

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity"> 

<RelativeLayout 
    android:layout_above="@+id/relativeBottom" 
    android:layout_width="match_parent" 
    android:layout_alignParentTop="true" 
    android:layout_height="match_parent"> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"></ListView> 
</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/relativeBottom" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true"> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAlignment="center" 
     android:text="Новая заметка"/> 
    </RelativeLayout> 
</RelativeLayout> 
+1

但是你并不需要第二层'RelativeLayout',它们的属性可以放在最后的视图中。现在他们只是增加你的布局绘图时间。 – AndroidEx 2015-04-04 16:39:19

+0

@ Android777你是对的,但我只是回答了他问哪个问题要添加两个布局,总之感谢您的评论。 – 2015-04-04 17:59:17

0

在我理解需要什么的时候,根本不需要相对布局。只需要LinearLayout即可实现,价格便宜。

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

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 

    <Button 
     android:id="@+id/myButton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="22dp" 
     android:text="New Note" 
     android:layout_marginBottom="5dp" 
     /> 
</LinearLayout>