将视图放在中间,并将顶部和底部安装在剩余空间中

时间:2013-07-16 00:13:45

标签: android android-layout

我创建了一个squareView,当放在布局上时会生成一个具有屏幕宽度长度的方形视图

我有3个视图squareView和2个其他视图让我们称之为view1和view2

我希望squareView位于屏幕的中心,view1和view2适合squareView顶部和底部的剩余空白区域

我到目前为止所尝试的内容没有成功

我试过的是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ice"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:background="@drawable/frame1"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:paddingBottom="4dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="4dp" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/sky" >

        <Button
            android:id="@+id/undo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_margin="5dp"
            android:background="@drawable/undobutton" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/undo"
            android:layout_toRightOf="@+id/menu"
            android:contentDescription="@string/app_name"
            android:src="@drawable/lines" />

        <Button
            android:id="@+id/menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_margin="5dp"
            android:background="@drawable/menubutton" />
    </RelativeLayout>
</LinearLayout>

<com.google.ads.AdView
    android:id="@+id/ad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    ads:adSize="BANNER"
    ads:adUnitId="a151e1f9140523a"
    ads:loadAdCreate="true" >
</com.google.ads.AdView>

<layouts.Board
    android:id="@+id/board1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" >
</layouts.Board>

请任何帮助。

2 个答案:

答案 0 :(得分:0)

检查一下,这应该有效

<View
    android:id="@+id/view1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/viewCenter"
    android:background="#FF0000" />

<ImageView
    android:id="@+id/viewCenter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/ic_launcher" />

<View
    android:id="@+id/view2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/viewCenter"
    android:background="#FF0000" />

答案 1 :(得分:0)

您可以通过两种方式执行此操作:RelativeLayout或LinearLayout。

使用RelativeLayout:

<RelativeLayout xmlns:android="..."
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<com.your.package.SquareView
    android:id="@+id/viewCenter"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/ic_launcher" />

<View
    android:id="@+id/view1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_above="@+id/viewCenter"
    android:layout_alignParentTop="true"
    android:background="#FF0000" />

<View
    android:id="@+id/view2"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_below="@+id/viewCenter"
    android:layout_alignParentBottom="true"
    android:background="#FF0000" />

</RelativeLayout>

使用LinearLayout:

<LinearLayout xmlns:android=...
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#FF0000" />

 <View
    android:id="@+id/view1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#FF0000" />

<com.your.package.SquareView
    android:id="@+id/viewCenter"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

<View
    android:id="@+id/view2"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#FF0000" />

</LinearLayout>