1

这是我在这里发布的第一个问题,所以如果我遗漏任何内容,请给我机会更新我的问题!

所以我是一个(相对)经验丰富的 swift 应用程序开发人员,在 App Store 上有几个应用程序。

目前,我正在努力将这些应用程序转移到 Google Play 商店,并且很难让任何事情都像在 swift 中一样顺利地发生。我决定从一个空白的应用程序重新启动我的整个第一个项目,并在此过程中提出问题以建立它。

我的应用程序正在使用一个 Main Activity 来控制由 bottomBar 导航项控制的 4-5 个片段。我想限制片段视图空间,使其成为:

片段视图顶部 = 标题栏底部

fragment_view_bottom = bottomBar_navigation_top

这样,顶部和底部的后面就不会隐藏任何东西。

我尝试了几种不同的布局约束选项,但都没有正常工作。甚至在片段内添加一个空的底部栏导航项,以便我可以将其限制在顶部,但它仍然不起作用!

任何帮助将不胜感激,谢谢!

片段.xml 代码:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragment_home">

    <android.support.v7.widget.AppCompatImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/newsImage"
        app:layout_constraintBottom_toTopOf="@id/navigation"/>

</android.support.constraint.ConstraintLayout>

这是我的 main_activity.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@color/colorAccent"
    android:id="@+id/container">

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:layout_gravity="bottom"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</FrameLayout>

编辑 09.20.2018:

这就是我在主要活动中实现片段之间切换的方式

MainActivity.kt 代码:

    class HomeActivity : AppCompatActivity(){

    lateinit var toolbar: ActionBar

    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_home -> {
                toolbar.title = "Home"
                val homeFragment = HomeFragment.newInstance()
                openFragment(homeFragment)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_map -> {
                toolbar.title = "Map"
                val localFragment = LocalFragment.newInstance()
                openFragment(localFragment)
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)

        toolbar = supportActionBar!!
        val bottomNavigation: BottomNavigationView = findViewById(R.id.navigation)
        bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
        bottomNavigation.selectedItemId = R.id.navigation_home



    private fun openFragment(fragment: Fragment) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.container, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }
}
4

1 回答 1

2

问题是:您正在用容器替换片段。放入FrameLayoutmain_activity.xml从中替换片段。喜欢,

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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:background="@color/colorAccent">

        <FrameLayout
            android:id="@+id/frmFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@id/navigation"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="0dp"
            android:layout_marginStart="0dp"
            android:layout_gravity="bottom"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>
    </android.support.constraint.ConstraintLayout>

而不是改变你的openFragment代码喜欢

  private fun openFragment(fragment: Fragment) {
    val transaction = supportFragmentManager.beginTransaction()
    transaction.replace(R.id.frmFragment, fragment)
    transaction.addToBackStack(null)
    transaction.commit()
  }
于 2018-09-20T08:50:08.923 回答