我有一个在我的应用程序中实现的底部导航 View 。 它有 3 个图标:
map Activity 、 fragment A、 fragment B 如下所示:
这就是我的 MapsActivity 的样子
所以我已经成功地显示了 fragment A 和 B。但似乎我在 xml 中的某个地方搞乱了 fragment (我是新人,所以我仍在尝试围绕 xml 的布局进行思考) ,如果我单击屏幕顶部搜索栏来自 MapsActivity 的位置,则搜索栏将显示为我的 fragment 是“透明”的,但您实际上无法在 fragment 上看到搜索栏。
这就是我的意思: 我如何让我的 fragment A 和 B 完全覆盖下面的 Activity ,以便它们不“透明”
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 中像这样显示我的 fragment 。
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()
}
...
...
}
我遇到的另一个问题将在解决当前帖子的问题后解决,但是有人知道当我单击导航栏中的“ map Activity ”图标时如何关闭当前 fragment 吗?
最佳答案
为 fragment 的所有父布局添加背景,如下所示
<?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"
添加到 fragment View
因为这将使 fragment 响应点击,这可能会解决“您的点击”问题
像这样
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>
关于Android - fragment 是 "transparent",我可以点击它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54065286/