将按钮一半放在一个布局父级中,另一半放在另一个布局中 [英] Place buttons half in one layout parent and half in another

查看:80
本文介绍了将按钮一半放在一个布局父级中,另一半放在另一个布局中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下布局,它工作正常,并给了我结果.我想要的结果是将屏幕分为两部分,并将按钮各占一半:

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>

我唯一的问题是我希望android:id="@+id/relative_layout_top占用android:id="@+id/relative_layout_bottom的1.5倍的空间,并且仍将按钮的一半留在android:id="@+id/relative_layout_bottom中,将一半留在android:id="@+id/relative_layout_bottom中.目前,两半的大小相同.有什么想法或建议如何做吗?

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?

以下是图形表示:

推荐答案

您必须同时将LinearChild设置为重心,将子代置于中心,将权重2设置为LinearLayout的顶部,将权重1设置为LinearLayout的底部,然后将按钮置于两个LinearLayout中使用了FrameLayout.

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>

这篇关于将按钮一半放在一个布局父级中,另一半放在另一个布局中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆