RelativeLayout里面的FrameLayout

时间:2013-05-27 03:01:19

标签: android xml

我有两个布局,一个只是一个背景,另一个布局在它的中心占据了大约一半的屏幕。我有我认为可以做的代码,但是当我运行时,我所包含的视图会占用整个屏幕,我无法弄清楚为什么会发生这种情况。

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

<include
    android:layout_width="500dp"
    android:layout_height="500dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    layout="@layout/activity_main" />

</RelativeLayout>

第二个xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000" >

<FrameLayout
    android:id="@+id/frmPreview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center" >
</FrameLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="left"
    android:gravity="center_vertical"
    android:orientation="vertical" >
</LinearLayout>

<ImageView
    android:id="@+id/imgShoot"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="bottom|center"
    android:background="@drawable/aperture_closing" />

2 个答案:

答案 0 :(得分:0)

尝试使用重量。

android:layout_weight="1"

答案 1 :(得分:0)

您无法在layout标记上指定<include>以外的任何属性,因为它将完全被您所包含的布局的根元素替换,并且它们将被删除。此外,您不需要RelativeLayout来集中孩子。出于性能原因,最好使用FrameLayout。

<强>父

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

  <include layout="@layout/activity_main" />

</RelativeLayout>

<强>儿童

<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="500dp"
  android:layout_height="500dp"
  android:layout_gravity="center"
  android:background="#00000000">

  <FrameLayout
    android:id="@+id/frmPreview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center">
  </FrameLayout>

  <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="left"
    android:gravity="center_vertical"
    android:orientation="vertical">
  </LinearLayout>

  <ImageView
    android:id="@+id/imgShoot"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="bottom|center"
    android:background="@drawable/aperture_closing"/>

</FrameLayout>

请注意,如果父FrameLayout不包含<include>标记以外的任何其他视图,则可以安全地跳过父版式,直接使用子布局。每个活动的根目录都有一个FrameLayout。