【问题标题】:Move View to "next line" if it doesn't fit如果不合适,将视图移动到“下一行”
【发布时间】:2021-08-27 14:21:36
【问题描述】:

所以,这个想法:我有两个TextViews,第一个可以扩展它想要的任何内容,第二个总是 5 个字符(时间)。问题在于第一个TextView 可以轻松地将第二个推出屏幕。

所以,我需要的是可调整的LinearLayout,或者可能是一些GridLayout,如果它不适合父母,它会在某种第二行上移动第二个TextView

例如,您可以在 Viber 和 WhatsApp 中观看消息气泡。感谢您的建议。

更新 1

这是我现在拥有的 XML(仅消息部分)

              <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/messageBox"
                android:layout_gravity="center_vertical"
                android:gravity="center_vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="14sp"
                    android:layout_gravity="center_vertical"
                    android:gravity="center_vertical"
                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
                    android:textColor="@color/black"
                    android:text='@{mess.message}'/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical|end"
                    android:gravity="center_vertical|end"
                    android:paddingLeft="8dp"
                    android:textSize="12sp"
                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
                    android:text='@{Utils.parseMillsToHoursAndMins(mess.date)}'/>
            </LinearLayout>

更新 2

所以我将 layout_weight 添加到第一个 TextView,这有助于解决我的第一个问题,但现在我有了新问题。这两个TextViews 在LinearLayout 中,在另一个LinearLayout 和另一个TextView 中。父 LinearLayout 将宽度设置为 wrap_content 所以如果顶部 TextView 将大于 2 TextViews 它将导致子 LinearLayout 小于它的父,并且第二个 TextView (从那个 2)不会t 在父级的末尾。但是当孩子LinearLayout 更大时,一切似乎都可以。我知道这很复杂,所以这是 XML

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="0dp"
            android:id="@+id/contentPanel"
            app:bringToFront="@{true}"
            android:orientation="vertical"
            android:background="@{(mess.isMine?@drawable/chat_bubble_right:@drawable/chat_bubble_left)}">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text='@{!mess.authorRole.equals("Client")?(mess.authorRole + " - " + mess.author):mess.author}'
                android:textColor='@{mess.authorRole.equals("Lawyer")?@color/colorPrimary:mess.authorRole.equals("Admin")?@color/red:@color/green}'
                android:textSize="12sp"
                android:id="@+id/author"
                android:fontFamily="sans-serif-medium"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/messageBox">
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:textSize="14sp"
                    android:layout_gravity="center_vertical"
                    android:gravity="center_vertical"
                    android:layout_weight="0.7"
                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
                    android:textColor="@color/black"
                    android:text='@{mess.message}'/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:paddingLeft="8dp"
                    android:textSize="12sp"
                    android:gravity="bottom|end"
                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
                    app:checkFit="@{false}"
                    android:text='@{Utils.parseMillsToHoursAndMins(mess.date)}'/>
            </LinearLayout>
        </LinearLayout>

【问题讨论】:

  • 把代码放在这里,这样会很容易
  • 你看过weightLinearLayout吗?
  • 显示xml代码
  • @DavidMedenjak 现在我将第一个视图的权重设置为 0.7,一切都开始好了。但在这种情况下,第二个视图不在父视图的末尾。

标签: android android-layout


【解决方案1】:

实现这种行为的新方法是使用带有Flow 的ConstraintLayout。下面是一个使用示例:

<androidx.constraintlayout.helper.widget.Flow
                        android:id="@+id/socialsButtonsFlow"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="6dp"
                        app:flow_horizontalGap="8dp"
                        app:flow_verticalGap="4dp"
                        app:flow_wrapMode="aligned"
                        app:flow_horizontalStyle="spread_inside"
                        app:constraint_referenced_ids="vkButton,twitterButton,facebookButton,youtubeButton,instagramButton,odnoklassnikiButton,tiktokButton"
                        app:layout_constraintEnd_toEndOf="@id/socialsLabel"
                        app:layout_constraintStart_toStartOf="@+id/socialsLabel"
                        app:layout_constraintTop_toBottomOf="@+id/socialsLabel" />

对于小屏幕,它看起来像这样:

【讨论】:

    【解决方案2】:

    我不确定这是否会对您有所帮助,但是: 使用:https://github.com/ApmeM/android-flowlayout

    <org.apmem.tools.layouts.FlowLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    
    </org.apmem.tools.layouts.FlowLayout>
    

    您可以在 FlowLayout 中放置您的视图,如果不合适,它将自动移至下一行。

    【讨论】:

    • 这几乎就是我想要的!也许您知道如何使其中的第二个TextView 对齐视图的右侧,如果它被移到下一行?
    • 我不确定,但如果有可用空间,它肯定会自动放入一行,否则它会移动到下一行。
    • 是的,它在下一行移动第二个视图,但它似乎在行的开头,而我希望它在最后。将第二个 View layout_width 设置为 match_parent 没有帮助。
    • 什么是第二视图?我的意思是,如果第二个视图是相对布局,并且在此布局内放置文本视图。在这种情况下,您可以将相对布局宽度设置为 match_parent 和此布局内的文本视图将宽度 wrap_content 与 android:layout_alignParentRight="true" 设置为文本视图。希望它对你有用。这只是想法,你可以尝试不同的观点。
    • 库本身的另一个解决方案:要更改布局方向,请使用以下代码 xmlns:f="schemas.android.com/apk/res/your.namespace" f:layoutDirection="rtl"
    【解决方案3】:

    如果你的文本视图在线性布局中,你可以添加 weightSum 方法

    猜你喜欢
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    • 2020-03-31
    相关资源
    最近更新 更多