在布局中进行
这可以使用ConstraintLayout 修复:
<?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 在相同的宽度上,所以从逻辑上讲这是无法实现的,因为当宽度的总和大于屏幕宽度时,某些视图会贪婪。
因此,您决定这样做wrap_content,直到按钮在不超出屏幕末端的情况下换行。
但是现在TextView 的宽度不应该是wrap_content;相反,它应该是一些固定大小;因为如果它仍然是wrap_content,它会在Button 上变得贪婪,并将其踢出屏幕或像你的情况一样被挤压。
因此,我们需要以编程方式做一些工作来了解屏幕宽度,因此我们将 TextView 限制为等式:
TextView_maxWidth = screenWidth - buttonWidth
解决方案 1:使用LinearLayout
如果我们在布局上设置TextView 文本,现在按钮将被挤压,我们无法以编程方式确定按钮的wrap_content 大小,因此在此解决方案中,textView 文本在我们之后以编程方式设置获得了 Button 的大小。
布局(没什么花哨的):
<?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");
}
});
}
解决方案 2:使用ConstraintLayout
与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);
}
});
}
预览: