在android中动态添加按钮的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在android中动态添加按钮的问题相关的知识,希望对你有一定的参考价值。
动态添加按钮的问题,如何让按钮自动进入下一行
像这样 :
我想要的输出:
我得到的输出:
这是我的代码:
LinearLayout ll = (LinearLayout)view.findViewById(R.id.layout);
Button btn = new Button(getContext());
btn.setText("Button1");
btn.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Button btn1 = new Button(getContext());
btn1.setText("Button2");
btn1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Button btn2 = new Button(getContext());
btn2.setText("Button3");
btn2.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Button btn3 = new Button(getContext());
btn3.setText("Button4");
btn3.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Button btn4 = new Button(getContext());
btn4.setText("Button5");
btn4.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ll.addView(btn);
ll.addView(btn1);
ll.addView(btn2);
ll.addView(btn3);
ll.addView(btn4);
在XML中:
<LinearLayout
android:orientation="horizontal"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>
我是Android新手,谢谢大家:)
答案
使用我新设计的PredicateLayout来自动将按钮移动到下一行,如下所示
XML
<PredicateLayout
android:orientation="horizontal"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Java的
PredicateLayout ll = (PredicateLayout)view.findViewById(R.id.layout);
Button btn = new Button(getContext());
btn.setText("Button1");
btn.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Button btn1 = new Button(getContext());
btn1.setText("Button2");
btn1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Button btn2 = new Button(getContext());
btn2.setText("Button3");
btn2.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Button btn3 = new Button(getContext());
btn3.setText("Button4");
btn3.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Button btn4 = new Button(getContext());
btn4.setText("Button5");
btn4.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ll.addView(btn);
ll.addView(btn1);
ll.addView(btn2);
ll.addView(btn3);
ll.addView(btn4);
另一答案
请将LinearLayout方向设置为垂直
<LinearLayout
android:orientation="vertical"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>
另一答案
LinearLayout linearLayoutSubParent = new LinearLayout(AddNewContactActivity.this);
linearLayoutSubParent.setOrientation(LinearLayout.HORIZONTAL);
linearLayoutSubParent.setWeightSum(100f);
LinearLayout.LayoutParams linearLayoutSubParentParams =
new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 90f); // you can add multiple view, by providing weight.
linearLayoutSubParent.setLayoutParams(linearLayoutSubParentParams);
linearLayoutSubParent.setPadding(0, 0, 0, 0);
final Button button = new Button(this);
LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
editTextParams.setMargins(0, 10, 0, 0);
button.setLayoutParams(editTextParams);
button.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.list_item_Big_text_size));
button.setText("Text");
button.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
button.setEnabled(false);
linearLayoutSubParent.addView(button);
linearLayoutParent.addView(linearLayoutSubParent);
<LinearLayout
android:id="@+id/linearLayoutParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
以上是关于在android中动态添加按钮的问题的主要内容,如果未能解决你的问题,请参考以下文章