【问题标题】:Place buttons between two layouts in android frame layout在android框架布局中的两个布局之间放置按钮
【发布时间】:2016-09-02 10:12:48
【问题描述】:

我正在尝试将具有两个 ImageButton 的 RelativeLayout 放置在已经具有 ImageView 和 LinearLayout 的 FrameLayout 中。我必须以这样一种方式放置相对布局,即 ImageButton 的垂直上半部分应与 ImageView 重叠,而下半部分应与 LinearLayout 重叠。

我现在的布局是这样的:

我当前的xml是:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#f2f2f2"
    android:elevation="8dp"
    android:orientation="vertical">


    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#fff"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="180dp"
                    android:src="@drawable/fire_icon" />


                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:background="@color/colorblue"
                    android:orientation="vertical"
                    android:padding="10dp"
                    android:layout_margin="10dp">

                    <TextView
                        android:id="@+id/title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_weight="1"
                        android:text="Need Help"
                        android:textColor="@color/colortitletext"
                        android:textSize="18sp"
                        android:textStyle="bold" />


                    <TextView
                        android:id="@+id/description"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:text="Hello, I am trying to create a android layout."
                        android:textColor="@color/colorheadtext"
                        android:textSize="18sp" />

                </LinearLayout>


            </LinearLayout>
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center|right"
                android:layout_marginRight="15dp">

                <ImageView
                    android:id="@+id/imageView4"
                    android:layout_width="60dp"
                    android:layout_height="50dp"
                    android:layout_alignParentTop="true"
                    android:layout_toLeftOf="@+id/imageView3"
                    android:layout_toStartOf="@+id/imageView3"
                    android:src="@drawable/ic_menu_camera" />

                <ImageView
                    android:id="@+id/imageView3"
                    android:layout_width="60dp"
                    android:layout_height="50dp"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:src="@drawable/ic_menu_gallery" />

            </RelativeLayout>
        </FrameLayout>


    </android.support.v7.widget.CardView>

</LinearLayout>

预期的布局是:

【问题讨论】:

  • 看看我的答案.......

标签: android android-layout layout android-framelayout android-relativelayout


【解决方案1】:

看看我编辑你的布局......

我认为这就是你想要的............

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#f2f2f2"
    android:elevation="8dp"
    android:orientation="vertical">


    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#fff"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="180dp"
                    android:src="@drawable/fire_icon"/>


                <LinearLayout 
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:background="#AACCAA"
                    android:orientation="vertical"
                    android:padding="10dp"
                    android:layout_margin="10dp">

                    <TextView
                        android:id="@+id/title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_weight="1"
                        android:text="Need Help"
                        android:textColor="@color/colortitletext"
                        android:textSize="18sp"
                        android:textStyle="bold" />


                    <TextView
                        android:id="@+id/description"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:text="Hello, I am trying to create a android layout."
                        android:textColor="@color/colorheadtext"
                        android:textSize="18sp" />

                </LinearLayout>


            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#f2f2f2"
                android:layout_marginTop="160dp"
                android:layout_marginLeft="200dp">

                <ImageView
                    android:id="@+id/imageView4"
                    android:layout_width="60dp"
                    android:layout_height="50dp"
                    android:src="@drawable/ic_menu_gallery"/>


                <ImageView
                    android:id="@+id/imageView3"
                    android:layout_width="60dp"
                    android:layout_height="50dp"
                    android:src="@drawable/ic_menu_camera"/>

            </LinearLayout>
        </FrameLayout>


    </android.support.v7.widget.CardView>

</LinearLayout>

享受编码............

【讨论】:

  • 谢谢苏希尔。但这可以在不使用边距的情况下完成吗?我的意思是使用某种相对定位来实现这一点。
  • 框架布局只在边距上工作,没有边距有很多问题,比如手机不同的像素,可见性因设备而异等。我认为没有边距的框架布局不可能设置它的视图。
猜你喜欢
  • 2016-06-24
  • 1970-01-01
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多