首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将片段中的布局项约束到主活动底部导航的顶部?

将片段中的布局项约束到主活动底部导航的顶部?
EN

Stack Overflow用户
提问于 2018-09-20 07:41:12
回答 1查看 1.6K关注 0票数 2

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

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

目前,我正致力于将这些应用程序转移到Google商店,并且很难让任何事情顺利地发生,就像斯威夫特一样。我决定从一个空白的应用程序中重新启动我的整个第一个项目,并在这个过程中问一些问题,这样就可以建立起来了。

我的应用程序使用一个主要活动来控制由bottomBar导航项控制的4-5个片段。我想限制片段的视图空间,以便:

fragment_view_top = titlebar_bottom

fragment_view_bottom = bottomBar_navigation_top

这样,任何东西都不会隐藏在顶部和底部。

我尝试过几个不同的布局约束选项,但是没有什么是正确工作的。甚至可以在片段中添加一个空的底部栏导航项,这样我就可以限制到它的顶部,而且它仍然不起作用!

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

fragment.xml代码:

代码语言:javascript
复制
    <?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代码:

代码语言:javascript
复制
<?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代码:

代码语言:javascript
复制
    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()
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-20 08:50:08

问题是:您要用容器替换碎片。将FrameLayout放入main_activity.xml并替换其中的片段。喜欢,

代码语言:javascript
复制
 <?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代码

代码语言:javascript
复制
  private fun openFragment(fragment: Fragment) {
    val transaction = supportFragmentManager.beginTransaction()
    transaction.replace(R.id.frmFragment, fragment)
    transaction.addToBackStack(null)
    transaction.commit()
  }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52420014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档