Android-视图中的换行按钮
我试图在Android中使按钮以线性布局进行包装,但它们只是继续在视图的右侧(屏幕截图中显示的单词应该是“HELLO”,所以我希望“O”下拉到下一行) 我以编程方式添加按钮,但即使将它们编码到XML布局文件中,它们仍然不会包装。下面是带有LinearLayout容器的布局文件,我正在动态地向其中添加按钮:Android-视图中的换行按钮,android,android-button,Android,Android Button,我试图在Android中使按钮以线性布局进行包装,但它们只是继续在视图的右侧(屏幕截图中显示的单词应该是“HELLO”,所以我希望“O”下拉到下一行) 我以编程方式添加按钮,但即使将它们编码到XML布局文件中,它们仍然不会包装。下面是带有LinearLayout容器的布局文件,我正在动态地向其中添加按钮: <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com
<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="match_parent"
android:layout_height="match_parent"
app:layout_constrainedWidth="true"
tools:context=".LetterTileView">
<LinearLayout
android:id="@+id/TilesContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constrainedWidth="true"
android:orientation="horizontal">
</LinearLayout>
Context context = this;
LinearLayout layout = (LinearLayout) findViewById(R.id.TilesContainer);
LayoutParams params = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
params.setMargins(50, 50, 0, 0);
for (int i=0;i<wordLength;i++) {
Button tileButton = new Button(this);
tileButton.setLayoutParams(params);
tileButton.setText(wordStringtoLetters[i]);
tileButton.setId(i);
tileButton.setBackgroundResource(R.drawable.tile_button);
tileButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 36);
layout.addView(tileButton);
}
下面是我用来创建和添加平铺按钮的代码:
<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="match_parent"
android:layout_height="match_parent"
app:layout_constrainedWidth="true"
tools:context=".LetterTileView">
<LinearLayout
android:id="@+id/TilesContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constrainedWidth="true"
android:orientation="horizontal">
</LinearLayout>
Context context = this;
LinearLayout layout = (LinearLayout) findViewById(R.id.TilesContainer);
LayoutParams params = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
params.setMargins(50, 50, 0, 0);
for (int i=0;i<wordLength;i++) {
Button tileButton = new Button(this);
tileButton.setLayoutParams(params);
tileButton.setText(wordStringtoLetters[i]);
tileButton.setId(i);
tileButton.setBackgroundResource(R.drawable.tile_button);
tileButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 36);
layout.addView(tileButton);
}
Context=this;
LinearLayout布局=(LinearLayout)findViewById(R.id.TileContainer);
LayoutParams params=新的LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容);
参数设置边距(50,50,0,0);
对于(int i=0;i首先,不需要使用ConstraintLayout,您可以使用LinearLayout作为父布局
然后,为了在一行中显示所有按钮,必须为XML中的LinearLayout设置权重,并为添加到其中的视图设置权重
xml文件应如下所示:
<LinearLayout
android:id="@+id/TilesContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="5"
app:layout_constrainedWidth="true"
android:orientation="horizontal">
</LinearLayout>
在代码中,您应该通过向LayoutParam添加,1.0f来设置每个视图的权重:
Context context = this;
LinearLayout layout = (LinearLayout) findViewById(R.id.TilesContainer);
LayoutParams params = new LayoutParams( LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,1.0f );
params.setMargins(50, 50, 0, 0);
for (int i=0;i<wordLength;i++) {
Button tileButton = new Button(this);
tileButton.setLayoutParams(params);
tileButton.setText(wordStringtoLetters[i]);
tileButton.setId(i);
tileButton.setBackgroundResource(R.drawable.tile_button);
tileButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 36);
layout.addView(tileButton);
}
Context=this;
LinearLayout布局=(LinearLayout)findViewById(R.id.TileContainer);
LayoutParams params=新的LayoutParams(LayoutParams.WRAP_内容,
LayoutParams.WRAP_内容,1.0f);
参数设置边距(50,50,0,0);
对于(int i=0;i我最后使用了FrimBox布局,这对于Nikos来说非常有用。感谢那些提供建议的人!因为你已经使用了一个约束布局,为什么不考虑流?谢谢,Nikos,我来研究一下。