无法点击 FAB 按钮的下半部分(结合自定义底部导航栏)
Can't click on bottom half of FAB button(combine with CustomBottomNavigationBar)
我创建了一个自定义底部导航栏(遵循这个建议:)
布局显示如我所料,但我仍然无法点击 fab 按钮的下半部分。
这是我的布局代码:
<RelativeLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:background="@color/grey_background"
android:orientation="vertical">
<nvlan.solocoding.exercisetraining.main.CustomBottomNavigationView
android:id="@+id/custom_bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:elevation="0dp"
app:itemTextColor="@color/bottom_nav_color"
app:itemIconTint="@color/bottom_nav_color"
tools:targetApi="lollipop" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:backgroundTint="@color/bottom_navigation_pressed"
android:id="@+id/map_button"
android:layout_width="@dimen/_48sdp"
android:layout_height="@dimen/_48sdp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginBottom="@dimen/_15sdp"
android:clickable="true"
android:elevation="@dimen/_2sdp"
android:contentDescription="@string/map"
android:focusable="true"
tools:targetApi="lollipop" />
</RelativeLayout>
我正在使用海拔高度,可点击,但它仍然无法正常工作。有没有什么方法可以实现我可以完全单击 FloatingActionButton 的想法?
这样设计,其中 <nvlan.solocoding.exercisetraining.main.CustomBottomNavigationView>
位于内部布局中。这通常是因为当我们使用第三方库时,它们在使用核心 android 组件时可能无法很好地交互。
因此,使用此类组件的安全方法是将它们包裹在不同的布局中,这样它们就不会出现在同一布局层次结构中。
<RelativeLayout
android:id="@+id/coordinator_layout_outer"
android:layout_width="match_parent"
android:layout_height="match_parent"
<RelativeLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:background="@color/grey_background"
android:orientation="vertical">
<nvlan.solocoding.exercisetraining.main.CustomBottomNavigationView
android:id="@+id/custom_bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:elevation="0dp"
app:itemTextColor="@color/bottom_nav_color"
app:itemIconTint="@color/bottom_nav_color"
tools:targetApi="lollipop" />
</RelativeLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:backgroundTint="@color/bottom_navigation_pressed"
android:id="@+id/map_button"
android:layout_width="@dimen/_48sdp"
android:layout_height="@dimen/_48sdp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginBottom="@dimen/_15sdp"
android:clickable="true"
android:elevation="@dimen/_2sdp"
android:contentDescription="@string/map"
android:focusable="true"
/>
<RelativeLayout>
看到 Mr.Narendra_Nath 回答后,我做了这个技巧并且成功了(将 fab 按钮放在 relativeLayout-@+id/coordinator_layout" 的一侧,而不是与 BottomNavigationBar 在同一布局层) .可能是因为BottomNavigationBar总是在父视图的顶层所以当你把另一个视图放在BottomNavigationBar的同一个视图组时,它会被放在BottomNavigationBar的后面。这是我的代码:
<androidx.constraintlayout.widget.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:orientation="vertical"
android:background="@color/grey_background">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.85" />
<RelativeLayout
app:layout_constraintTop_toTopOf="@id/guideline_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:background="@color/grey_background"
android:orientation="vertical">
<nvlan.solocoding.exercisetraining.main.CustomBottomNavigationView
android:id="@+id/custom_bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemTextColor="@color/bottom_nav_color"
app:itemIconTint="@color/bottom_nav_color" />
</RelativeLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:layout_constraintTop_toTopOf="@id/guideline_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:backgroundTint="@color/bottom_navigation_pressed"
android:id="@+id/map_button"
android:layout_width="@dimen/_48sdp"
android:layout_height="@dimen/_48sdp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginBottom="@dimen/_15sdp"
android:clickable="true"
android:contentDescription="@string/map"
android:focusable="true" />
</androidx.constraintlayout.widget.ConstraintLayout>
我创建了一个自定义底部导航栏(遵循这个建议:
这是我的布局代码:
<RelativeLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:background="@color/grey_background"
android:orientation="vertical">
<nvlan.solocoding.exercisetraining.main.CustomBottomNavigationView
android:id="@+id/custom_bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:elevation="0dp"
app:itemTextColor="@color/bottom_nav_color"
app:itemIconTint="@color/bottom_nav_color"
tools:targetApi="lollipop" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:backgroundTint="@color/bottom_navigation_pressed"
android:id="@+id/map_button"
android:layout_width="@dimen/_48sdp"
android:layout_height="@dimen/_48sdp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginBottom="@dimen/_15sdp"
android:clickable="true"
android:elevation="@dimen/_2sdp"
android:contentDescription="@string/map"
android:focusable="true"
tools:targetApi="lollipop" />
</RelativeLayout>
我正在使用海拔高度,可点击,但它仍然无法正常工作。有没有什么方法可以实现我可以完全单击 FloatingActionButton 的想法?
这样设计,其中 <nvlan.solocoding.exercisetraining.main.CustomBottomNavigationView>
位于内部布局中。这通常是因为当我们使用第三方库时,它们在使用核心 android 组件时可能无法很好地交互。
因此,使用此类组件的安全方法是将它们包裹在不同的布局中,这样它们就不会出现在同一布局层次结构中。
<RelativeLayout
android:id="@+id/coordinator_layout_outer"
android:layout_width="match_parent"
android:layout_height="match_parent"
<RelativeLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:background="@color/grey_background"
android:orientation="vertical">
<nvlan.solocoding.exercisetraining.main.CustomBottomNavigationView
android:id="@+id/custom_bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:elevation="0dp"
app:itemTextColor="@color/bottom_nav_color"
app:itemIconTint="@color/bottom_nav_color"
tools:targetApi="lollipop" />
</RelativeLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:backgroundTint="@color/bottom_navigation_pressed"
android:id="@+id/map_button"
android:layout_width="@dimen/_48sdp"
android:layout_height="@dimen/_48sdp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginBottom="@dimen/_15sdp"
android:clickable="true"
android:elevation="@dimen/_2sdp"
android:contentDescription="@string/map"
android:focusable="true"
/>
<RelativeLayout>
看到 Mr.Narendra_Nath 回答后,我做了这个技巧并且成功了(将 fab 按钮放在 relativeLayout-@+id/coordinator_layout" 的一侧,而不是与 BottomNavigationBar 在同一布局层) .可能是因为BottomNavigationBar总是在父视图的顶层所以当你把另一个视图放在BottomNavigationBar的同一个视图组时,它会被放在BottomNavigationBar的后面。这是我的代码:
<androidx.constraintlayout.widget.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:orientation="vertical"
android:background="@color/grey_background">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.85" />
<RelativeLayout
app:layout_constraintTop_toTopOf="@id/guideline_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:background="@color/grey_background"
android:orientation="vertical">
<nvlan.solocoding.exercisetraining.main.CustomBottomNavigationView
android:id="@+id/custom_bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemTextColor="@color/bottom_nav_color"
app:itemIconTint="@color/bottom_nav_color" />
</RelativeLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:layout_constraintTop_toTopOf="@id/guideline_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:backgroundTint="@color/bottom_navigation_pressed"
android:id="@+id/map_button"
android:layout_width="@dimen/_48sdp"
android:layout_height="@dimen/_48sdp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginBottom="@dimen/_15sdp"
android:clickable="true"
android:contentDescription="@string/map"
android:focusable="true" />
</androidx.constraintlayout.widget.ConstraintLayout>