代码之家  ›  专栏  ›  技术社区  ›  Glenncito

无论我如何更改约束,我的片段都会显示在工具栏下面

  •  0
  • Glenncito  · 技术社区  · 6 年前

    无论我如何添加约束,包含RecyclerView的片段都不会发生任何变化。

    我添加了一个工具栏:

    在MainActivity中创建

    覆盖fun oncreate(savedinstancestate:bundle?){ super.oncreate(保存到状态) setContentView(r.layout.activity_main)//要查找不一致项,请执行以下操作 val demo=demodatabuilder()。 //将工具栏设置为支持操作栏 设置支持操作栏(工具栏) //现在获取支持操作栏 val actionbar=支持actionbar //设置工具栏标题/应用标题 行动吧!!.title=“敬畏” //设置操作栏/工具栏子标题 actionBar.subtitle=“应用程序副标题” //设置操作栏高度 actionbar.elevation=4.0f toolbar.setnavigationicon(R.drawable.ic_arrow_back_black_24dp) toolbar.setNavigationonClickListener->onBackpressed() StartCategoryHome(保存到状态) } < /代码>

    活动\main.xml

    <?xml version=“1.0”encoding=“utf-8”?gt;
    <android.support.constraint.constraintlayout(限制布局)
    xmlns:android=“http://schemas.android.com/apk/res/android”
    xmlns:app=“http://schemas.android.com/apk/res-auto”
    xmlns:tools=“http://schemas.android.com/tools”
    android:layout_width=“匹配父级”
    android:layout_height=“匹配父级”
    android:id=“@+id/root_layout”>
    
    
    <android.support.v7.widget.toolbar
    android:id=“@+id/工具栏”
    android:layout_width=“匹配父级”
    android:layout_height=“0dp”
    android:background=“?属性/颜色主要“
    安卓:minheight=“57dp”
    安卓:padding=“1dp”
    app:layout_constraintop_totopof=“父级”
    app:subtitletextcolor=“f5fbff”
    app:titletextcolor=“fff”/>
    
    
    片段
    android:id=“@+id/categoylist\u片段”
    android:name=“za.co.aweza.awezamediphrases.fragments.categorylistfragment”
    android:layout_width=“匹配父级”
    android:layout_height=“包装内容”
    android:layout_margintop=“112dp”
    app:layout_constraintop_tobottomof=“@+id/工具栏”
    工具:layout=“@layout/fragment_hcp_category”/>gt;
    
    </android.support.constraint.constraintlayout>
    < /代码> 
    
    

    您会注意到,在上面的布局代码中,我有一个非常牵强的约束,我试图使用它。

    以下是它在设计视图中的显示方式:

    但是不管我做什么尝试,片段中RV的第一个单元大部分位于工具栏下面。我错过了什么?在主要活动中

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)//TODO find inconsistencies
        val demo = DemoDataBuilder()
    
    
        // Set the toolbar as support action bar
        setSupportActionBar(toolbar)
    
        // Now get the support action bar
        val actionBar = supportActionBar
    
        // Set toolbar title/app title
        actionBar!!.title = "AwezaMed"
    
        // Set action bar/toolbar sub title
        actionBar.subtitle = "App subtitle"
    
        // Set action bar elevation
        actionBar.elevation = 4.0F
    
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp)
        toolbar.setNavigationOnClickListener { _ -> onBackPressed()}
    
        startCategoryHome(savedInstanceState)
    
    }
    

    活动\main.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root_layout">
    
    
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="?attr/colorPrimary"
        android:minHeight="57dp"
        android:padding="1dp"
        app:layout_constraintTop_toTopOf="parent"
        app:subtitleTextColor="#f5fbff"
        app:titleTextColor="#fff" />
    
    
    <fragment
        android:id="@+id/categoyList_fragment"
        android:name="za.co.aweza.awezamediphrases.fragments.CategoryListFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="112dp"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        tools:layout="@layout/fragment_hcp_category" />
    
    </android.support.constraint.ConstraintLayout>
    

    您会注意到在上面的布局代码中,我尝试使用了一个非常牵强的约束。

    以下是它在设计视图中的显示方式: enter image description here

    但是不管我做什么尝试,片段中RV的第一个单元大部分位于工具栏下面。我错过了什么?

    2 回复  |  直到 6 年前
        1
  •  1
  •   EliodeBeirut    6 年前

    不要将此片段放在XML中:

    `<FrameLayout
        android:id="@+id/frameLayout_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@id/toolbar" />`
    

    使用此函数将框架布局替换为所需的任何片段:

     ` fun openFragment(fragment: Fragment) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.frameLayout_fragment_container, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }`
    
        2
  •  0
  •   Glenncito    6 年前

    @EliodeBeirut的回答很有效,但我发现将layout activity_main.xml从约束布局更改为线性布局也很有效。