ScrollView 不适合 parent 的宽度

ScrollView doesn't fit width of parent

我正在尝试用新的 Fragment 填充我的 parent Activity。它在设计器模式下看起来很好,但在 运行 时会缩小到任意宽度。设置 fillViewport=true 似乎没有什么不同。

设计器中的 SrollView

模拟器中的 SrollView

其他选项卡包含 RecyclerViewFragments 的网格。他们遇到了同样的问题,直到我为 XML 中的列定义了固定宽度,并以编程方式将列数设置为屏幕的总宽度除以固定列宽 (noOfColumns=dpWidth/columnWidth+1) .


Activity

<?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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/gray"
    tools:context="com.jamesjmtaylor.weg2015.TabBarActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:orientation="vertical">

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/gray"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/navigation"/>
    </LinearLayout>

    <FrameLayout
        android:id="@+id/fragmentFrameLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="@dimen/bottomNavigationViewHeight"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </FrameLayout>

    <FrameLayout
        android:id="@+id/hudFrameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

</android.support.constraint.ConstraintLayout>

ScrollView

<ScrollView
    android:id="@+id/cardsSetupFragment"
    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:fillViewport="true"
    tools:context=".cardsTab.CardsFragment">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".cardsTab.CardsActivity">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:text="@string/equipment_flash_cards"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="16dp"
            android:text="@string/choose_set_difficulty"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView2"/>

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="@string/choose_set_type"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="@+id/textView5"
            app:layout_constraintTop_toBottomOf="@+id/qtyTextView"/>

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="@string/choose_set_quantity"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="@+id/radioGroup"
            app:layout_constraintTop_toBottomOf="@+id/radioGroup"/>

        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:checkedButton="@+id/easyRadioButton"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView3">

            <RadioButton
                android:id="@+id/easyRadioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingEnd="16dp"
                android:paddingStart="8dp"
                android:text="@string/easy"/>

            <RadioButton
                android:id="@+id/mediumRadioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingEnd="16dp"
                android:paddingStart="8dp"
                android:text="@string/medium"/>

            <RadioButton
                android:id="@+id/hardRadioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingEnd="16dp"
                android:paddingStart="8dp"
                android:text="@string/hard"/>

        </RadioGroup>

        <SeekBar
            android:id="@+id/qtySeekBar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:max="100"
            android:progress="10"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/qtyTextView"
            app:layout_constraintTop_toTopOf="@+id/qtyTextView"/>

        <TextView
            android:id="@+id/qtyTextView"
            android:layout_width="64dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:text="@string/placeholder_card_qty"
            app:layout_constraintStart_toStartOf="@+id/textView5"
            app:layout_constraintTop_toBottomOf="@+id/textView5"/>

        <ToggleButton
            android:id="@+id/airToggleButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textOff="@string/title_air"
            android:textOn="@string/title_air"
            app:layout_constraintEnd_toEndOf="@+id/landToggleButton"
            app:layout_constraintStart_toEndOf="@+id/seaToggleButton"
            app:layout_constraintTop_toTopOf="@+id/seaToggleButton"/>

        <ToggleButton
            android:id="@+id/seaToggleButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textOff="@string/title_sea"
            android:textOn="@string/title_sea"
            app:layout_constraintEnd_toStartOf="@+id/airToggleButton"
            app:layout_constraintStart_toStartOf="@+id/gunsToggleButton"
            app:layout_constraintTop_toBottomOf="@+id/gunsToggleButton"/>

        <ToggleButton
            android:id="@+id/landToggleButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textOff="@string/title_land"
            android:textOn="@string/title_land"
            app:layout_constraintEnd_toEndOf="@+id/qtySeekBar"
            app:layout_constraintStart_toEndOf="@+id/gunsToggleButton"
            app:layout_constraintTop_toTopOf="@+id/gunsToggleButton"/>

        <ToggleButton
            android:id="@+id/gunsToggleButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:checked="true"
            android:textOff="@string/weapons"
            android:textOn="@string/weapons"
            app:layout_constraintEnd_toStartOf="@+id/landToggleButton"
            app:layout_constraintHorizontal_chainStyle="spread_inside"
            app:layout_constraintStart_toStartOf="@+id/textView4"
            app:layout_constraintTop_toBottomOf="@+id/textView4"
            />

        <Button
            android:id="@+id/startButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="24dp"
            android:background="@color/black"
            android:text="@string/start"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/seaToggleButton"
            app:layout_constraintVertical_bias="1.0"/>
    </android.support.constraint.ConstraintLayout>
</ScrollView>

当您将所有 4 个约束都赋予一个视图然后使其宽度和高度为 0dp

<?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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray"
tools:context="com.jamesjmtaylor.weg2015.TabBarActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="vertical">

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/gray"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation"/>
</LinearLayout>

<FrameLayout
    android:id="@+id/fragmentFrameLayout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="@dimen/bottomNavigationViewHeight"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

</FrameLayout>

<FrameLayout
    android:id="@+id/hudFrameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

改变这个https://gist.github.com/jamesjmtaylor/ef5ea59197cb52d1ecb46fa101b7af51 用上面的代码 我已将 FrameLayout 的宽度从 match_parent

更改为 0dp