我的问题:
我正在尝试使用ConstraintLayout完成Master-Detail-Layout。
我的布局文件如下所示:
<?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.support.v7.widget.Toolbar
android:id="@id/toolbar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="@string/preferences"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/mainFragment"
app:layout_constraintVertical_chainStyle="spread"/>
<FrameLayout
android:id="@+id/mainFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/detailsFragment"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:layout_constraintWidth_max="400dp"
app:layout_constraintWidth_min="300dp"/>
<FrameLayout
android:id="@+id/detailsFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintLeft_toRightOf="@id/mainFragment"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"/>
</android.support.constraint.ConstraintLayout>
我试图完成,左边的FrameLayout在300-400dp之间传播,而右边的FrameLayout正在填充剩余的空间。这可以完成,还是我必须为左侧布局设置固定宽度?以下蓝图展示了我的问题。右侧FrameLayout只复制左侧布局的尺寸。
答案 0 :(得分:0)
问题在于您使用了链条。为了使此布局正常工作,请使用以下代码替换mainFragment
:
<FrameLayout
android:id="@+id/mainFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:layout_constraintWidth_max="400dp"
app:layout_constraintWidth_min="300dp" />
更改:更改右侧约束以适合父级(而不是使用detailsFragment
进行链接)并添加水平偏差,以便从左边缘开始。您还可以从detailsFragment
。
专业提示:链样式只应在屏幕最左侧和/或顶侧的视图中定义。其他链条样式被忽略了。