LinearLayout:防止最后一个孩子被前面的大文本视图推出或挤压 [英] LinearLayout: prevent last child from getting pushed out or squeezed by large textview before it

查看:77
本文介绍了LinearLayout:防止最后一个孩子被前面的大文本视图推出或挤压的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个LinearLayout,里面有两个孩子。第一个是动态内容的TextView,第二个是按钮。我的问题是,按钮被推出其父按钮,或者被挤压到不再可见的程度。我希望TextView认识到与第二个孩子一起在其父代中没有更多的空间,并开始新的行,而不是窃取第二个孩子所需的空间。但是,按钮应该在文本旁边,而不是默认情况下一直位于的右侧。

我尝试了许多不同的方法,但都不起作用,在下面的代码中,我只是发布我想要工作的最小示例,而不是我已经尝试的不同的更改。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sdrndesfntzrfndtzmufzksbnsfdn stnbhdsfbns sth st ömcfz,frznz"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>
总而言之:我希望TextView占据几行内容,而不是占据按钮的空间。当然,行数应该是动态的,这取决于视图中的文本。该按钮应紧挨着文本视图,并随着文本变大而向右移动。

以下是我想要的内容的直观表示:

这是我得到的。

设置文本视图的maxwidth是一个简单的解决方案,但在更复杂和动态的布局中,您并不总是知道最大宽度是多少。因此,如果有一个没有硬编码的最大宽度的解决方案就好了。

推荐答案

您可以尝试FlexboxLayouthttps://github.com/google/flexbox-layout

<com.google.android.flexbox.FlexboxLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:alignItems="center"
    app:alignContent="center"
    app:flexDirection="row"
    app:flexWrap="nowrap">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sdrndesfntzrfndtzmufzksbnsfdn stnbhdsfbns sth st ömcfz,frznz dfgsfsdfsdf"
        app:layout_flexShrink="10000"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"/>
</com.google.android.flexbox.FlexboxLayout>

app:layout_flexShrink="10000"是正确调整元素大小的方法。

EDTbuild.gradle配置:

allprojects {
  repositories {
    ...
    maven { url "https://maven.google.com" }
  }
}

dependencies {
  ...
  implementation 'com.google.android.flexbox:flexbox:3.0.0'
}

这篇关于LinearLayout:防止最后一个孩子被前面的大文本视图推出或挤压的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆