Android - 片段是 "transparent",我可以点击它
Android - Fragment is "transparent", i'm able to click through it
我有一个在我的应用程序中实现的底部导航视图。
它有 3 个图标:
地图Activity,片段A,片段B像这样:
这是我的地图Activity的样子
所以我设法显示了片段 A 和 B。但似乎我在 xml 的某处弄乱了片段(我是新手,所以我仍在努力思考在 xmls 的布局周围),如果我在搜索栏来自地图 Activity 的屏幕顶部单击,则搜索栏将显示为我的片段 "transparent" ,但您实际上看不到片段上的搜索栏。
我的意思是:
我如何让我的片段 A 和 B 完全覆盖下面的 activity,这样它们就不会 "transparent"
activity_maps.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/fragmentContainer">
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="@id/place_autocomplete_fragment"
android:layout_above="@id/bottom_navigation"
/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
app:menu="@menu/bottom_nav_menu"
android:background="@color/colorPrimaryDark"
android:layout_width="match_parent"
app:itemIconTint="@color/common_google_signin_btn_text_dark_default"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:layout_marginBottom="0dp"
android:layout_alignParentLeft="true" android:layout_marginLeft="0dp"
android:layout_alignParentStart="true" android:layout_marginStart="0dp"/>
</RelativeLayout>
fragment_a.xml
<?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="match_parent"
android:orientation="vertical"
android:background="@color/common_google_signin_btn_text_dark_pressed">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment A"
android:gravity="center_vertical|center_horizontal"/>
</LinearLayout>
我在MapsActivity.kt.
中这样显示我的片段
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener {item->
when(item.itemId){
R.id.nav_map -> {
Log.d(TAG, "map pressed")
// if there's a fragment, close it
return@OnNavigationItemSelectedListener true
}
R.id.nav_A -> {
Log.d(TAG, "fragment A pressed")
replaceFragment(FragmentA())
return@OnNavigationItemSelectedListener true
}
R.id.nav_B -> {
Log.d(TAG, "fragment B pressed")
replaceFragment(FragmentB())
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
val bottomNavigation: BottomNavigationView = findViewById(R.id.bottom_navigation)
bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}
private fun replaceFragment(fragment: Fragment){
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragmentContainer, fragment)
fragmentTransaction.commit()
}
...
...
}
我遇到的另一个问题,我会在解决当前 post 的问题后解决,但是有谁知道如何在我单击地图 Activity 图标时关闭当前片段在导航栏中?
像下面这样为片段的所有父布局添加背景
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
*like this android:background="#fff"
android:orientation="vertical"
android:id="@+id/fragmentContainer">
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="@id/place_autocomplete_fragment"
android:layout_above="@id/bottom_navigation"
/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
app:menu="@menu/bottom_nav_menu"
android:background="@color/colorPrimaryDark"
android:layout_width="match_parent"
app:itemIconTint="@color/common_google_signin_btn_text_dark_default"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:layout_marginBottom="0dp"
android:layout_alignParentLeft="true" android:layout_marginLeft="0dp"
android:layout_alignParentStart="true" android:layout_marginStart="0dp"/>
</RelativeLayout>
这应该有效并且
另一个修复方法是将 android:clickable="true"
添加到片段视图
因为这将使片段响应可能修复 'your click through' 问题的点击
像这样
Fragment_a.xml
<?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="match_parent"
android:clickable="true"
android:orientation="vertical"
android:background="@color/common_google_signin_btn_text_dark_pressed">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment A"
android:gravity="center_vertical|center_horizontal"/>
</LinearLayout>
我有一个在我的应用程序中实现的底部导航视图。 它有 3 个图标:
地图Activity,片段A,片段B像这样:
这是我的地图Activity的样子
所以我设法显示了片段 A 和 B。但似乎我在 xml 的某处弄乱了片段(我是新手,所以我仍在努力思考在 xmls 的布局周围),如果我在搜索栏来自地图 Activity 的屏幕顶部单击,则搜索栏将显示为我的片段 "transparent" ,但您实际上看不到片段上的搜索栏。
我的意思是:
activity_maps.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/fragmentContainer">
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="@id/place_autocomplete_fragment"
android:layout_above="@id/bottom_navigation"
/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
app:menu="@menu/bottom_nav_menu"
android:background="@color/colorPrimaryDark"
android:layout_width="match_parent"
app:itemIconTint="@color/common_google_signin_btn_text_dark_default"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:layout_marginBottom="0dp"
android:layout_alignParentLeft="true" android:layout_marginLeft="0dp"
android:layout_alignParentStart="true" android:layout_marginStart="0dp"/>
</RelativeLayout>
fragment_a.xml
<?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="match_parent"
android:orientation="vertical"
android:background="@color/common_google_signin_btn_text_dark_pressed">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment A"
android:gravity="center_vertical|center_horizontal"/>
</LinearLayout>
我在MapsActivity.kt.
中这样显示我的片段class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener {item->
when(item.itemId){
R.id.nav_map -> {
Log.d(TAG, "map pressed")
// if there's a fragment, close it
return@OnNavigationItemSelectedListener true
}
R.id.nav_A -> {
Log.d(TAG, "fragment A pressed")
replaceFragment(FragmentA())
return@OnNavigationItemSelectedListener true
}
R.id.nav_B -> {
Log.d(TAG, "fragment B pressed")
replaceFragment(FragmentB())
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
val bottomNavigation: BottomNavigationView = findViewById(R.id.bottom_navigation)
bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}
private fun replaceFragment(fragment: Fragment){
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragmentContainer, fragment)
fragmentTransaction.commit()
}
...
...
}
我遇到的另一个问题,我会在解决当前 post 的问题后解决,但是有谁知道如何在我单击地图 Activity 图标时关闭当前片段在导航栏中?
像下面这样为片段的所有父布局添加背景
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
*like this android:background="#fff"
android:orientation="vertical"
android:id="@+id/fragmentContainer">
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="@id/place_autocomplete_fragment"
android:layout_above="@id/bottom_navigation"
/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
app:menu="@menu/bottom_nav_menu"
android:background="@color/colorPrimaryDark"
android:layout_width="match_parent"
app:itemIconTint="@color/common_google_signin_btn_text_dark_default"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:layout_marginBottom="0dp"
android:layout_alignParentLeft="true" android:layout_marginLeft="0dp"
android:layout_alignParentStart="true" android:layout_marginStart="0dp"/>
</RelativeLayout>
这应该有效并且
另一个修复方法是将 android:clickable="true"
添加到片段视图
因为这将使片段响应可能修复 'your click through' 问题的点击
像这样
Fragment_a.xml
<?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="match_parent"
android:clickable="true"
android:orientation="vertical"
android:background="@color/common_google_signin_btn_text_dark_pressed">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment A"
android:gravity="center_vertical|center_horizontal"/>
</LinearLayout>