Android - fragment 是 "transparent",我可以点击它

标签 android android-fragments android-activity kotlin bottomnavigationview

我有一个在我的应用程序中实现的底部导航 View 。 它有 3 个图标:

map Activity 、 fragment A、 fragment B 如下所示: enter image description here

这就是我的 MapsActivity 的样子

所以我已经成功地显示了 fragment A 和 B。但似乎我在 xml 中的某个地方搞乱了 fragment (我是新人,所以我仍在尝试围绕 xml 的布局进行思考) ,如果我单击屏幕顶部搜索栏来自 MapsActivity 的位置,则搜索栏将显示为我的 fragment 是“透明”的,但您实际上无法在 fragment 上看到搜索栏。

这就是我的意思: enter image description here 我如何让我的 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/

相关文章:

Android Activity 太忙无法设置 TextView 文本?

android - 如何区分是否调用 onDestroy() 作为配置更改序列的一部分?

android - RxAndroid,如何检测可观察对象是否已完成发射

Android 动画留下伪影

android - 在包含 fragment 的主 Activity 中拉动刷新

android - 如何从 BroadCastReceiver 更新 Activity 的 UI

iphone - 媒体查询在 Android 浏览器上失败

Android - 定期后台服务 - 建议

java - 从 Eclipse Java 项目创建 Android 应用程序

java - 我可以将接口(interface)传递给 Fragment 包吗?