【问题标题】:Constrain layout items in fragments to top of main activity bottom bar navigation?将片段中的布局项约束到主要活动底部栏导航的顶部?
【发布时间】:2018-09-20 07:41:12
【问题描述】:

这是我在这里发布的第一个问题,所以如果我遗漏了什么,请给我机会更新我的问题!

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

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

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

fragment_view_top = titlebar_bottom

fragment_view_bottom = bottomBar_navigation_top

这样,顶部和底部的后面就再也没有隐藏任何东西了。

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

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

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

【问题讨论】:

  • 如何添加片段?请发帖。
  • 你的意思是我如何实现从片段到片段的变化?我会相应地更新我的问题@AbhayKoradiya
  • 检查我的答案

标签: xml android-fragments kotlin android-constraintlayout bottombar


【解决方案1】:

问题是:您正在用容器替换片段。将FrameLayout 放入main_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()
  }

【讨论】:

  • 太棒了。总是小事哈哈。对您提供的代码稍作更改,在 BottomViewNavigation 部分中的 app:layout_constraintRight_toRightOf="parent" 之后,我们需要实现 app:menu="@menu/navigation" 以便它知道要使用什么菜单!真棒工作 1000 颗金星送给你!
猜你喜欢
  • 1970-01-01
  • 2022-08-19
  • 2019-12-29
  • 1970-01-01
  • 2018-12-02
  • 1970-01-01
  • 1970-01-01
  • 2021-04-30
  • 2021-10-01
相关资源
最近更新 更多