我有一个LinearLayout,里面有两个孩子。第一个是具有动态内容的TextView,第二个是按钮。我的问题是,按钮被从父按钮中按下,或者被压缩到一个不再可见的点。我希望TextView认识到它的父母和第二个孩子在一起没有更多的空间,并且开始一个新的行,而不是窃取第二个孩子所需要的空间。但是,Button应该在文本旁边,而不是默认情况下一直到右侧。
我尝试了很多不同的不起作用的东西,在下面的代码中,我只是发布我想要工作的最小示例,而不是我已经尝试过的不同的更改。
<?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使用几行代码,而不是使用按钮的空间。当然,行数应该是动态的,这取决于视图中的文本。Button应该直接在文本视图旁边,并在文本变得更大时向右移动。
下面是我想要的东西的可视化表示:


这就是我得到的。

设置文本视图的maxwidth是一个简单的解决方案,但是在更复杂和动态的布局中,您并不总是知道最大宽度是多少。所以如果没有硬编码的maxWidths解决方案就好了。
发布于 2021-06-21 15:46:27
您可以尝试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"是正确调整元素大小的方法。
EDT build.gradle配置:
allprojects {
repositories {
...
maven { url "https://maven.google.com" }
}
}
dependencies {
...
implementation 'com.google.android.flexbox:flexbox:3.0.0'
}发布于 2021-06-21 22:39:19
在布局上做
可以使用ConstraintLayout通过以下方式修复此问题:
packed水平链<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sdrndesfntzrfndtzmufzksbnsfdn stnbhdsfbns sth st ömcfz,frznz"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/button"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/textView"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>动态地执行它
如果您仍然需要使用LinearLayout,那么您可以编程完成它。由于您需要在相同宽度下wrap_content TextView和Button,所以在逻辑上这是无法实现的,因为当宽度之和大于屏幕宽度时,另一个视图会贪婪。
因此,您决定这样做,直到按钮在不关闭屏幕结束的情况下对其宽度进行包装。
但是现在TextView的宽度不应该是wrap_content,而应该是一些固定的大小;因为如果它仍然是wrap_content,那么它就会在Button上贪婪,然后把它踢出屏幕,或者像您的情况那样被压缩。
因此,我们需要以编程的方式进行一些工作,以了解屏幕宽度,因此我们将把TextView限制在等式上:
TextView_maxWidth = screenWidth - buttonWidthLinearLayout 解决方案1:使用
如果我们在布局上设置TextView文本,现在按钮将被压缩,并且我们无法以编程方式确定按钮的wrap_content大小,因此在这个解决方案中,textView文本是在获得按钮大小之后以编程方式设置的。
布局(没什么花哨):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>行为:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout root = findViewById(R.id.root);
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
root.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
}
int width = root.getWidth();
Button button = findViewById(R.id.button);
int buttonWidth = button.getWidth();
int maxTextWidth = width - buttonWidth;
TextView textView = findViewById(R.id.textView);
textView.setMaxWidth(maxTextWidth);
textView.setText("drndesfntzrfndtzmufzksbnsfdn stnbhdsfbns sth st ömcfz,frznz");
}
});
}ConstraintLayout 解决方案2:使用
与LinearLayout不同,ConstraintLayout可以强制按钮大小为wrap_content,而无需压缩,因此我们可以在布局上设置TextView文本。
布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sdrn desf ntzrf ndtz mufzk sbnsf dn sdrnde sf ntzrfn dtz mufzks bns fdn sd rn de sfnt zrf ndtzmufzk sbnsfdnstn bhdsfbns sth st ömcfz, frznz"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintStart_toEndOf="@id/textView"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>行为:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ConstraintLayout root = findViewById(R.id.root);
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
root.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
}
int width = root.getWidth();
Button button = findViewById(R.id.button);
int buttonWidth = button.getWidth();
int maxTextWidth = width - buttonWidth;
TextView textView = findViewById(R.id.textView);
textView.setMaxWidth(maxTextWidth);
}
});
}预览:



发布于 2021-06-21 13:27:39
您可以将TextView和Button的宽度设置为0dp并设置weigths,如下所示:
<?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="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="sdrndesfntzrfndtzmufzksbnsfdn stnbhdsfbns sth st ömcfz,frznz"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>在本例中,TextViews宽度为3/4s,按钮宽度为父级宽度的1/4,但您可以通过播放权重将其设置为任意大小。
https://stackoverflow.com/questions/68068546
复制相似问题