【问题标题】:Android - Fragment is "transparent", i'm able to click through itAndroid - 片段是“透明的”,我可以点击它
【发布时间】:2019-01-06 19:47:10
【问题描述】:

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

Map Activity, Fragment A, Fragment B 像这样:

这就是我的 MapsActivity 的样子

所以我设法显示了片段 A 和 B。但似乎我在我的 xml 中的某个地方用片段搞砸了(我是新人,所以我仍在尝试围绕 xmls 的布局进行思考) ,如果我点击屏幕顶部的搜索栏来自 MapsActivity,然后搜索栏将显示为我的片段是“透明的”,但您实际上看不到片段上的搜索栏。

这就是我的意思: 如何让我的片段 A 和 B 完全覆盖下面的活动,使它们不“透明”

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()
    }
...
...
}

我有另一个问题,我将在修复此当前帖子的问题后解决,但是有谁知道当我单击导航栏中的地图活动图标时如何关闭当前片段?

【问题讨论】:

标签: android android-fragments android-activity kotlin bottomnavigationview


【解决方案1】:

为片段的所有父布局添加背景,如下所示

<?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_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>

【讨论】:

  • 我的片段已经有了背景,我所说的“透明”是指虽然片段涵盖了下面活动的所有内容(据我所知),但我仍然能够以某种方式单击 MapsActivity xml 中的 BottomNavigationView 小部件
  • @Barcode 这曾经发生在我身上,但解决方法是为片段添加背景,因为它们之前是透明的,我们可以点击它
  • 谢谢!添加android:clickable="true" 为我工作
猜你喜欢
  • 2012-07-16
  • 1970-01-01
  • 2013-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-30
相关资源
最近更新 更多