如何在android中添加重叠按钮
我目前正在为android应用程序构建一个自定义按钮导航栏。它内部有几个可点击的部分(见下面的cod),中间有一个fab按钮,应该与导航栏的顶部重叠一半 我通过使用负边距并在每个父元素上将clipChildren设置为false来实现这一点。从视觉上看,它看起来就像我想要的那样,但导航栏外的fab按钮部分无法单击 我如何实现仍然可以点击的悬垂按钮 它应该是这样的: 这是我的测试代码。在母公司内部点击晶圆厂是有效的,而在外部点击不太有效 activity_main.xml如何在android中添加重叠按钮,android,overlap,floating-action-button,Android,Overlap,Floating Action Button,我目前正在为android应用程序构建一个自定义按钮导航栏。它内部有几个可点击的部分(见下面的cod),中间有一个fab按钮,应该与导航栏的顶部重叠一半 我通过使用负边距并在每个父元素上将clipChildren设置为false来实现这一点。从视觉上看,它看起来就像我想要的那样,但导航栏外的fab按钮部分无法单击 我如何实现仍然可以点击的悬垂按钮 它应该是这样的: 这是我的测试代码。在母公司内部点击晶圆厂是有效的,而在外部点击不太有效 activity_main.xml <?xml ve
<?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>
我现在很确定,这是不可能的方式,我想这样做。 不过,我找到了另一种很好的方法:只需将fab按钮放在菜单外,然后使用相对布局+负片填充等方法将其放置在正确的位置。以下是工作布局:
<?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>
<?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>