Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将视图放在中间,并将顶部和底部放在剩余空间中_Android_Android Layout - Fatal编程技术网

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

Android 将视图放在中间,并将顶部和底部放在剩余空间中,android,android-layout,Android,Android Layout,我创建了一个正方形视图,当放在布局上时,它会生成一个正方形视图,其长度与屏幕宽度相同 我有3个视图——squareView和2个其他视图——我们称它们为view1和view2 我希望squareView位于屏幕中央,view1和view2适合squareView顶部和底部的剩余空白空间 到目前为止我所尝试的都没有成功 我尝试的是: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http

我创建了一个正方形视图,当放在布局上时,它会生成一个正方形视图,其长度与屏幕宽度相同

我有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>


请提供任何帮助。

检查一下,应该可以

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

您可以通过两种方式执行此操作:相对布局或线性布局

与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 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>