如何在Android中添加重叠按钮

问题描述:

我目前正在为Android应用构建自定义按钮导航栏.它在内部包含几个可单击的部分(请参见下面的代码),中间包含一个fab按钮,该按钮应与导航栏的顶部重叠一半.

I am currently building a custom button navigation bar for an android app. It contains several clickable section inside (see cod below) and one fab button in the center that should overlap the top side of the navigation bar by half.

我做到了这一点,方法是使用负边距并将每个父元素的clipChildren设置为false.视觉上看起来就像我想要的一样,但是无法单击导航栏外部的fab按钮的一部分.

I managed to do this by using a negative margin and setting clipChildren to false on every parent element. Visually it looks just like I want it to but the part of the fab button outside of the navigation bar can't be clicked.

如何实现仍可点击的悬垂按钮?

How can I achieve that overhanging button that is still clickable?

它应该是这样的:

这是我的测试代码.在父级内部点击fab的工作正常,而在外部级进行的点击不是很多.

Here is my test code. Clicks on the fab inside the parent are working, outside not so much.

activity_main.xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:clipChildren="false">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:orientation="horizontal"
        android:clipChildren="false">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center"
            android:id="@+id/menu1">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_check"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Menu 1"/>

        </LinearLayout>

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

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_check"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Menu 2"/>

        </LinearLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center"
            android:clipChildren="false">

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fabMenu"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/ic_check"
                android:layout_marginTop="-20dp"
                android:layout_alignParentTop="true"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Menu 2"
                android:layout_alignParentBottom="true"/>

        </RelativeLayout>

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

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_check"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Menu 4"/>

        </LinearLayout>

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

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_check"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Menu 5"/>

        </LinearLayout>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

和MainActivity.java

And the MainActivity.java

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("MainActivity", "Started main activity");

        findViewById(R.id.menu1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("MenuListener", "Clicked on menu 1 - working!");
            }
        });

        findViewById(R.id.fabMenu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("MenuListener", "Clicked on fab - partly working!");
            }
        });
    }
}

我现在很确定,这是我想做的方式不可能的. 我发现了另一种运行良好的方法:只需将fab按钮放在菜单之外,并使用相对布局+负填充等将其放置在正确的位置即可.这是工作布局:

I am now pretty sure that it is just not possible the way I wanted to do it. I have found another way that works well though: just put the fab button outside of the menu and use something like a relative layout + the negative padding to position it just right. Here is the working layout:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_centerInParent="true" />

    <!-- just placed outside of the menu -->
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabMenu"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/ic_check"
        android:layout_marginTop="-20dp"
        android:layout_alignTop="@+id/menu"
        android:layout_centerHorizontal="true"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:id="@+id/menu">

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

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_check"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Menu 1"/>

        </LinearLayout>

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

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_check"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Menu 2"/>

        </LinearLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:clipChildren="false">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Menu 3"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"/>

        </RelativeLayout>

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

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_check"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Menu 4"/>

        </LinearLayout>

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

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_check"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Menu 5"/>

        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

和一张图片: