我对 Android 很陌生,我正在尝试在我的 android 应用程序中动态添加按钮,问题是它们是垂直显示的,而这应该是水平显示的。
我得到了什么:
我期待(和想要):
我正在使用的代码:
MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout ll = (LinearLayout)findViewById(R.id.main_linearlayout);
for(int x = 1; x <= 5 ; x++)
{
LinearLayout tmpLinearLayout = new LinearLayout(this);
tmpLinearLayout.setOrientation(LinearLayout.VERTICAL);
tmpLinearLayout.setLayoutParams( new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f));
tmpLinearLayout.getLayoutParams().height = 200;
ll.addView(tmpLinearLayout);
for(int i = 0;i<5;i++)
{
Button tmpButton = new Button(this);
tmpButton.setText("nr:" + i +" - " + x);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1.0f);
tmpLinearLayout.addView(tmpButton, lp);
}
}
}
布局(activity_main.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/main_linearlayout"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@drawable/background"
android:orientation="horizontal" >
</LinearLayout>
谁能解释它为什么这样做/纠正我?谢谢!