无论我如何更改约束,我的片段都会出现在工具栏下方
My Fragment appears underneath the Toolbar regardless of how I change constraints
无论我如何添加约束,包含 RecyclerView 的 Fragment 都没有任何变化。
我添加了一个工具栏:
OnCreate 在 MainActivity
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)
}
activity_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>
您会注意到,在上面的布局代码中,我尝试使用了一个非常牵强的约束。
这是它在 designView 中的显示方式:
但无论我尝试什么,Fragment 中 RV 的第一个单元格大部分位于工具栏下方。我错过了什么?
而不是片段将其放入您的 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()
}`
@EliodeBeirut 的回答有效,但我发现将布局 activity_main.xml 从约束布局更改为线性布局也同样有效。
无论我如何添加约束,包含 RecyclerView 的 Fragment 都没有任何变化。
我添加了一个工具栏:
OnCreate 在 MainActivity
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)
}
activity_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>
您会注意到,在上面的布局代码中,我尝试使用了一个非常牵强的约束。
这是它在 designView 中的显示方式:
但无论我尝试什么,Fragment 中 RV 的第一个单元格大部分位于工具栏下方。我错过了什么?
而不是片段将其放入您的 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()
}`
@EliodeBeirut 的回答有效,但我发现将布局 activity_main.xml 从约束布局更改为线性布局也同样有效。