[英]Dynamic layout for textview - android
我正在从服务器获取字符串数。
我想在下图所示的布局上显示它
每次我都会得到不同类型的字符串。 那么如何为textview创建动态布局?
可能是您正在寻找“ Flow Layout
。
使用此代码,我们可以在textview内部动态创建线性布局。 如果需要,只需将代码修改为上面要求的内容即可。
onCreate() Method :
private Button[] btnAdd ,btnQuestionPalete ; // GLobally
private LinearLayout layout_button ;
layout_button = (LinearLayout) findViewById(R.id.button_layout);
btnAdd = new Button[100] ;
LayoutParams param2 = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
btnQuestionPalete = new Button[100];
for (int i = 0; i < noOfButton; i++) {
linearQuestionPalettesRow = new LinearLayout(getApplicationContext());
linearQuestionPalettesRow.setOrientation(LinearLayout.VERTICAL);
linearQuestionPalettesRow.setLayoutParams(param2);
linearQuestionPalettesRow.setPadding(10, 10, 10, 10);
textView[i] = new TextView(getApplicationContext());
textView[i].setId(i+1);
textView[i].setText("test");
textView[i].setTextColor(Color.BLACK);
textView[i].setGravity(Gravity.CENTER_HORIZONTAL);
linearQuestionPalettesRow.addView(textView[i]);
layout_button.addView(linearQuestionPalettesRow);
}
xml code :
<LinearLayout
android:id="@+id/button_layout"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:orientation="horizontal"
>
</LinearLayout>
只需将字符串添加到array / arraylist并使之成为arrayadapter即可 ,然后将listView或DridView传递给它很容易
您可以使用表布局来动态实现此目的。 但我认为您希望以锯齿形的方式布局:
------------------ ------------------ ------------------ ------------------
因此您可以使用ListView
(高效)来完成此操作,但是如果您不希望使用此模式并且行数不超过5-10,则可以使用TableLayout
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.