Bar*_*ode 2 android android-fragments android-activity kotlin bottomnavigationview
我有一个在我的应用程序中实现的底部导航视图。它有 3 个图标:
这就是我的 MapsActivity 的样子
所以我已经成功地显示了片段 A 和 B。但似乎我在 xml 中的某个地方搞乱了片段(我是新人,所以我仍在尝试围绕 xml 的布局进行思考),如果我单击屏幕顶部搜索栏来自 MapsActivity 的位置,然后搜索栏将显示,就像我的片段是“透明”的,但您实际上无法在片段上看到搜索栏。
这就是我的意思:
我如何让我的片段 A 和 B 完全覆盖下面的活动,这样它们就不是“透明的”
活动地图.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>
Run Code Online (Sandbox Code Playgroud)
片段_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>
Run Code Online (Sandbox Code Playgroud)
我像这样在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()
}
...
...
}
Run Code Online (Sandbox Code Playgroud)
我遇到的另一个问题是在解决当前帖子的问题后我将解决的,但是有谁知道当我单击导航栏中的“地图活动”图标时如何关闭当前片段?
小智 5
向片段的所有父布局添加背景,如下所示
<?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>
Run Code Online (Sandbox Code Playgroud)
这应该可行,另一个修复方法是添加android:clickable="true"到片段视图,因为这将使片段响应点击,这可能会修复“您的点击”问题,例如
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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2929 次 |
| 最近记录: |