2015-04-04 98 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>