LinearLayout: prevent last child from getting pushed out or squeezed by large textview before it(LinearLayout:防止最后一个孩子被前面的大文本视图推出或挤压)
本文介绍了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
是一个简单的解决方案,但在更复杂和动态的布局中,您并不总是知道最大宽度是多少。因此,如果有一个没有硬编码的最大宽度的解决方案就好了。
推荐答案
您可以尝试FlexboxLayout
:https://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:防止最后一个孩子被前面的大文本视图推出或挤压的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:LinearLayout:防止最后一个孩子被前面的大文本视图推出或挤压


猜你喜欢
- Android - 拆分 Drawable 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01