【问题标题】:Content of View not shown completely when using a ConstraintLayout - Android使用 ConstraintLayout 时视图的内容未完全显示 - Android
【发布时间】:2018-02-04 08:24:16
【问题描述】:

我在参考线上方使用this 颜色选择器,并希望在参考线下方对齐文本视图(和其他视图),并通过使用带有以下选项的 ConstraintLayout 将它们绑定到颜色选择器的宽度:

app:layout_constraintRight_toRightOf="@+id/colorPicker"
app:layout_constraintLeft_toLeftOf="@+id/colorPicker"

(颜色选择器的宽度取决于可用的高度。)

我面临的问题是视图似乎恰好位于颜色选择器下方,但是,视图的内容未调整,因此未完全显示,如this 图片所示。

使用 ImageView 而不是 TextView 可以重现类似的行为。 RecyclerView 似乎正在工作,但是,到达列表开头或结尾时的“列表结束动画”放错了位置。

当使用其他视图而不是这个颜色选择器时,我不会遇到这个问题。

谁能解释这种行为以及如何解决它?

我为此使用的完整 xml 代码:

<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"
    tools:context="xxx.xxx.xxx.MainActivity">

    <com.rarepebble.colorpicker.ColorPickerView
        android:id="@+id/colorPicker"
        android:layout_width="wrap_content"
        android:layout_height="0dp"

        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/guideline"

        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintGuide_percent="0.60"
        android:orientation="horizontal"
        />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Hello World! Some chars in this TextView are cut on the right side."

        app:layout_constraintTop_toTopOf="@+id/guideline"
        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintRight_toRightOf="@+id/colorPicker"
        app:layout_constraintLeft_toLeftOf="@+id/colorPicker"
        />

</android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: android android-constraintlayout


    【解决方案1】:

    您是否尝试过使用LinearLayout?对于这样的问题,一个视图需要占据屏幕的百分比,使用layout_weight

    使用线性布局的解决方案

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical">
            <com.rarepebble.colorpicker.ColorPickerView
                android:id="@+id/colorPicker"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="6"
                />
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="4">
                <TextView
                    android:id="@+id/textView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Hello World! Some chars in this TextView are cut on the right side." />
            </FrameLayout>
    </LinearLayout>
    

    基于 cmets 编辑

    这样的事情应该可以工作。您可以根据FloatingActionButton 是否被点击,将View 的可见性更改为GONEVISIBLE

    <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.design.widget.FloatingActionButton
            android:id="@+id/floatingActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:fabSize="normal"
            android:layout_marginStart="16dp"
            app:layout_constraintLeft_toLeftOf="@+id/linearContainer"
            app:layout_constraintTop_toTopOf="@+id/linearContainer"
            android:layout_marginTop="16dp" />
    
        <View
            android:id="@+id/view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="0dp"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="0dp"
            android:layout_marginTop="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />
    
        <LinearLayout
            android:id="@+id/linearContainer"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginBottom="0dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginLeft="8dp"
            app:layout_constraintVertical_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent">
    
            <com.rarepebble.colorpicker.ColorPickerView
                android:id="@+id/colorPicker"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="6" />
    
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="4">
    
                <TextView
                    android:id="@+id/textView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Hello World! Some chars in this TextView are cut on the right side." />
            </FrameLayout>
        </LinearLayout>
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 是的,我在最新版本中使用 LinarLayout,但无法继续使用 LinarLayout 来获得新功能。
    • @JulianEggers 我同意让这些工作可能很棘手。但是您在这里谈论的是什么新功能?你能详细说明一下吗?
    • 当然。基本上,我在上部有一个动态大小的颜色选择器,我想在颜色选择器的左上角显示一个 FAB。 FAB 可以展开,并且应该在触摸屏幕任意位置后关闭(如 Inbox 的 FAB 或解释 here)。我尝试在顶层使用 View 来监听触摸事件并关闭 FAB。显然,FAB 及其子 FAB 不再可点击,因为它们必须留在 LinearLayout 中才能将它们定位在颜色选择器的角落。
    • @JulianEggers 听起来可行。检查我的答案的编辑。
    • 我希望 FAB 位于颜色选择器的左上角,但使用您的代码可以通过 app:layout_constraintLeft_toLeftOf="@+id/linearLayout"app:layout_constraintTop_toTopOf="@+id/linearLayout" 轻松完成。让我检查一下是否一切正常。
    猜你喜欢
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多