2016-12-02 105 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

相关问题