Android布局:如何将徽标放在两个元素的边框上?

时间:2017-08-04 14:57:34

标签: android android-layout

我制作了我的Android应用。客户的要求是将徽标放在底部工具栏中。问题是徽标应放在底部工具栏的一半上,一半放在内容上。问题是我不能使用负边距,所以我不能将徽标图像从内容中移出,也不能在工具栏上移动。看起来应该是这样的:

enter image description here

我制作了两个工具栏 - 透明和填充。但问题是我无法将内容的底部延伸到填充工具栏的顶部,因为它位于透明工具栏内。这是图片和代码。

enter image description here

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarTop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorBrandColor"
        app:popupTheme="@style/Theme.AppCompat.Light.DarkActionBar"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

            <ImageView
                android:id="@+id/imageViewTop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:src="@drawable/sponge_bob" />

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


    <FrameLayout
        android:id="@+id/content_frame_to_inflate"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf ="@+id/toolbarTop"
        app:layout_constraintBottom_toTopOf="@+id/toolbarBottomTransparent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarBottomTransparent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/content_frame_to_inflate"
        app:contentInsetStart="0dp">


        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" >

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbarBottomFilled"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorBrandColor"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_marginTop="32dp"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="64dp"
                android:src="@drawable/sponge_bob_logo"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </android.support.constraint.ConstraintLayout>

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


</android.support.constraint.ConstraintLayout>

如何正确布局?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情。

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarTop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#cdcdcd"
        app:popupTheme="@style/Theme.AppCompat.Light.DarkActionBar"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <ImageView
            android:id="@+id/imageViewTop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp" />

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


    <FrameLayout
        android:id="@+id/content_frame_to_inflate"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf ="@+id/toolbarTop"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#cdcdcd"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="8dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">



    </RelativeLayout>

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="#000000"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="10dp"/>

</RelativeLayout>

enter image description here

更新了布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarTop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#4984ad"
        app:popupTheme="@style/Theme.AppCompat.Light.DarkActionBar"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

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


    <FrameLayout
        android:id="@+id/content_frame_to_inflate"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbarTop"
        android:layout_above="@+id/bottomBar"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:src="@drawable/background"/>

    </FrameLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="#4984ad"
        android:id="@+id/bottomBar"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="8dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">



    </RelativeLayout>

<ImageView
    android:layout_width="200dp"
    android:layout_height="70dp"
    android:src="@drawable/SpongeBob_SquarePants_logo"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="25dp"/>

</RelativeLayout>

enter image description here