Place buttons half in one layout parent and half in another

懵懂的女人 提交于 2019-12-11 14:49:14

问题


I have the following layout, it works fine and gives me the result. The result I want is to divide the screen into two parts and place buttons half in each part:

   <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="200dp" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" >

                <RelativeLayout
                    android:id="@+id/relative_layout_top"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <ImageView
                        android:id="@+id/imageView1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentTop="true"
                        android:layout_marginTop="10dp"
                        android:scaleType="centerCrop" />
                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" >

                <RelativeLayout
                    android:id="@+id/relative_layout_bottom"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:background="#fdd372" >

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="20dp"
                        android:layout_marginTop="0dp"
                        android:text="Jason D&apos;Silva"
                        android:textColor="#696969"
                        android:textSize="22sp" />

                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignLeft="@+id/textView2"
                        android:layout_below="@+id/textView2"
                        android:layout_marginTop="05dp"
                        android:paddingBottom="05dp"
                        android:paddingTop="05dp"
                        android:text="Me and Christine"
                        android:textColor="#696969" />

                    <!--
                        <TextView
                        android:id="@+id/placeholder"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/textView4"
                        android:layout_marginTop="02dp" />
                    -->

                    <TextView
                        android:id="@+id/textView4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBaseline="@+id/textView3"
                        android:layout_alignBottom="@+id/textView3"
                        android:layout_alignParentRight="true"
                        android:layout_marginRight="14dp"
                        android:text="24 October 2014"
                        android:textColor="#696969" />
                </RelativeLayout>
            </LinearLayout>
        </LinearLayout>

        <Button
            android:id="@+id/imageButton1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:background="@drawable/icon_edit"
            android:textColor="#000000" />

        <Button
            android:id="@+id/imageButton2"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignTop="@+id/imageButton1"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@+id/imageButton1"
            android:background="@drawable/icon_person" />


    </RelativeLayout>

The only problem I have is that I want the android:id="@+id/relative_layout_top to take 1.5 times the space of android:id="@+id/relative_layout_bottom and still keep the buttons half in android:id="@+id/relative_layout_bottom and half in android:id="@+id/relative_layout_bottom. Currently both the halves are of the same size. Any ideas hints or suggestions how to do that?

Here is the graphical representation:


回答1:


Your have to set gravity center to you both LinearLayout which place child at center and set weight 2 to top LinearLayout and 1 to bottom LinearLayout and put button in two LinearLayout used FrameLayout.

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="vertical"
            android:gravity="center">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" >

                <RelativeLayout
                    android:id="@+id/relative_layout_top"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <ImageView
                        android:id="@+id/imageView1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentTop="true"
                        android:layout_marginTop="10dp"
                        android:scaleType="centerCrop" />
                </RelativeLayout>
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center">

            <RelativeLayout
                android:id="@+id/relative_layout_bottom"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="#fdd372" >

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="0dp"
                    android:text="Jason D&apos;Silva"
                    android:textColor="#696969"
                    android:textSize="22sp" />

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/textView2"
                    android:layout_below="@+id/textView2"
                    android:layout_marginTop="05dp"
                    android:paddingBottom="05dp"
                    android:paddingTop="05dp"
                    android:text="Me and Christine"
                    android:textColor="#696969" />

                <!--
                    <TextView
                    android:id="@+id/placeholder"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/textView4"
                    android:layout_marginTop="02dp" />
                -->

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textView3"
                    android:layout_alignBottom="@+id/textView3"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="14dp"
                    android:text="24 October 2014"
                    android:textColor="#696969" />
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="right"
        android:orientation="vertical">
        <View
            android:layout_width="1dp"
            android:layout_height="0dp"
            android:layout_weight="2"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/imageButton2"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/ic_launcher" />
            <Button
                android:id="@+id/imageButton1"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/ic_launcher"
                android:textColor="#000000" />

        </LinearLayout>
        <View
            android:layout_width="1dp"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>
</FrameLayout>


来源:https://stackoverflow.com/questions/26795475/place-buttons-half-in-one-layout-parent-and-half-in-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!