历览千载书,时时见遗烈。这篇文章主要讲述在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);
另一答案【在android中动态添加按钮的问题】请将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);
// youcan 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工作室
- 为什么Android Chrome中的边框会消失()
- 使用Live Id从Universal App登录到Web Api
- 为什么'src'出现在Angular App的地址栏中()
- 使用EF Database-First Application部署ASP.NET Web API
- 尝试从asp.net web api调用获取响应时android studio中的超时异常
- Android http发布到ASP.NET Web API不会插入数据
- SpringMVC配置applicationContext.xml应该导入some-servlet.xml
- 如何在android中解析查询搜索案例不敏感()