ScrollView不适合父级的宽度

时间:2018-03-19 13:03:25

标签: android xml android-fragments android-scrollview android-constraintlayout

我试图用新的Activity填充我的父Fragment。它在设计器模式下看起来很好,但在运行时会压缩到任意宽度。设置fillViewport=true似乎无法发挥作用。

设计器中的SrollView

ScrollView in designer mode

模拟器中的SrollView

ScrollView in emulator

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

活动

<?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
    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>

2 个答案:

答案 0 :(得分:2)

当您将所有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 用上面的代码 我已将matchLayout的宽度从match_parent

更改为0dp

答案 1 :(得分:0)

问题出在FrameLayout,标识为fragmentFrameLayout。您可以将宽度更改为0dp或将ConstraintLayout的版本更新为以后的版本,我相信该版本为1.1.0-beta5并离开match_parent。如果您可以使用测试版,我建议您同时执行这两项操作。

我还要提到主要活动布局中的LinearLayout不受限制。随附的文本视图具有无用的约束,因为它的直接父级不是ConstraintLayout。您可能只想将文本视图约束移动到其父LinearLayout