2013-05-27 22 views
0

У меня есть два макета, который будет только фоном, а другой, который будет в центре его, займет примерно половину экрана. У меня есть код, который, как я думал, будет делать, но представление, которое я включаю, охватывает весь экран, когда приложение запускается, и я не могу понять, почему это происходит.FrameLayout внутри RelativeLayout

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

+0

, на каком устройстве можно проверить этот код? – TheFlash

ответ

0

Попробуйте с использованием весов.

android:layout_weight="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 в своем корне.

Смежные вопросы