2016-12-02 107 views
0

我創建了一個小例子來解釋我的問題。在這個例子中,我創建了一個填充整個屏幕的RelativeLayout。和3個孩子:如何填充父級相對佈局中的所有空間?

  1. ID爲中間的FrameLayout必須填補所有空白區域沒有覆蓋上的FrameLayout(粉紅色)
  2. 的FrameLayout id爲頂部必須表現出中等以上(紅色)
  3. 的FrameLayout的頂部和底部ID爲底部必須出現如下圖所示中間(黃色)

佈局:

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

    <FrameLayout 
     android:id="@+id/top" 
     android:layout_width="100dp" 
     android:layout_height="24dp" 
     android:layout_above="@id/middle" 
     android:background="#FFFF0000" /> 

    <FrameLayout 
     android:id="@+id/middle" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#FFFF00FF" /> 

    <FrameLayout 
     android:id="@+id/bottom" 
     android:layout_width="100dp" 
     android:layout_height="24dp" 
     android:layout_below="@id/middle" 
     android:background="#FFFFFF00" />  

</RelativeLayout> 

截圖我的示例結果:

enter image description here

而我需要做的截圖:

enter image description here

+0

你有使用'RelativeLayout'? –

回答

0

好吧,這段代碼應該這樣做。

檢查出來:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<FrameLayout 
    android:id="@+id/top" 
    android:layout_width="100dp" 
    android:layout_height="24dp" 
    android:background="#FFFF0000" 
    android:layout_alignParentTop="true" /> 
<FrameLayout 
    android:id="@+id/bottom" 
    android:layout_width="100dp" 
    android:layout_height="24dp" 
    android:background="#FFFFFF00" 
    android:layout_alignParentBottom="true" /> 
<FrameLayout 
    android:id="@+id/middle" 
    android:layout_above="@id/bottom" 
    android:layout_below="@id/top" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:background="#FFFF00FF" /> 
</RelativeLayout> 
+0

感謝它的好解決方案 – Anton111111

0

我希望這將有助於你

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

    <FrameLayout 
     android:id="@+id/top" 
     android:layout_width="100dp" 
     android:layout_height="24dp" 
     android:background="#FFFF0000" 
     android:layout_alignParentTop="true" 
     /> 

    <FrameLayout 
     android:id="@+id/middle" 
     android:layout_above="@+id/bottom" 
     android:layout_below="@+id/top" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#FFFF00FF" /> 
    <FrameLayout 
     android:id="@+id/bottom" 
     android:layout_width="100dp" 
     android:layout_height="24dp" 
     android:background="#FFFFFF00" 
     android:layout_alignParentBottom="true" 
     /> 

</RelativeLayout> 
0

像這樣的東西應該工作:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <FrameLayout 
     android:id="@+id/middle" 
     android:layout_marginTop="24dp" 
     android:layout_marginBottom="24dp" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#FFFF00FF" /> 

    <FrameLayout 
     android:id="@+id/top" 
     android:layout_width="100dp" 
     android:layout_height="24dp" 
     android:background="#FFFF0000" /> 

    <FrameLayout 
     android:id="@+id/bottom" 
     android:layout_width="100dp" 
     android:layout_height="24dp" 
     android:layout_gravity="bottom" 
     android:background="#FFFFFF00" /> 

</FrameLayout> 
0

而不是使用RelativeLayout可以使用LinearLayout和利用layout_weight填補剩餘空間。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<FrameLayout 
    android:id="@+id/top" 
    android:layout_width="100dp" 
    android:layout_height="24dp" 
    android:background="#FFFF0000" /> 

<FrameLayout 
    android:id="@+id/middle" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:background="#FFFF00FF" 
    android:layout_weight="1"/> 

<FrameLayout 
    android:id="@+id/bottom" 
    android:layout_width="100dp" 
    android:layout_height="24dp" 
    android:background="#FFFFFF00" /> 

</LinearLayout> 
0

要做到這一點,相對佈局則需要對齊頂部和底部佈局,邊框,不是設置在它們之間關係的中間佈局。

見下文:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="pl.orbitemobile.myapplication.MainActivity"> 


      <FrameLayout 
       android:id="@+id/top" 
       android:layout_width="100dp" 
       android:layout_height="24dp" 
       android:layout_alignParentTop="true" 
       android:background="#FFFF0000" /> 

      <FrameLayout 
       android:id="@+id/middle" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_below="@id/top" 
       android:layout_above="@+id/bottom" 
       android:background="#FFFF00FF" /> 

      <FrameLayout 
       android:layout_width="100dp" 
       android:id="@+id/bottom" 
       android:layout_height="24dp" 
       android:layout_alignParentBottom="true" 
       android:background="#FFFFFF00" /> 

</RelativeLayout> 

效果:

enter image description here

相關問題