0
我在創建動態UI時遇到了一些問題。 我可以讓它與一個LinearLayout一起工作,但我不知道如何添加更多。動態UI - 如何將3個線性佈局添加到父線性佈局
這就是我現在所擁有的,它是一個帶3個按鈕的LinearLayout。
下面是代碼:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
maakLayout();
}
private void maakLayout() {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams layoutParams = new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(layoutParams);
setContentView(linearLayout);
for (int i = 0; i < 3; i++) {
Button button = new Button(this);
button.setText("Test");
button.setTag(i);
button.setBackgroundResource(R.drawable.border_red);
LayoutParams layoutTextParams = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// LayoutParams layoutImageParams =
// new LayoutParams(100,100);
layoutTextParams.topMargin = 5;
layoutTextParams.leftMargin = 5;
layoutTextParams.rightMargin = 5;
button.setLayoutParams(layoutTextParams);
linearLayout.addView(button);
}
}
我需要的是以下幾點: 而對於這個靜態的XML代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
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="com.example.test.MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border_red"
android:text="Test" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border_red"
android:text="Test" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border_red"
android:text="Test" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="144dp"
android:layout_marginRight="5dp"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/border_blue"
android:text="Test" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border_red"
android:text="Test" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border_red"
android:text="Test" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border_red"
android:text="Test" />
</LinearLayout>
</LinearLayout>
一旦我知道如何添加其他線性佈局我很好,我想,謝謝你的幫助。
對不起,我沒有得到什麼問題?問題是什麼? – olyv
看看我需要的示例沒有靜態XML – user2103237
這是你在找什麼? http://stackoverflow.com/questions/5731487/how-to-add-views-to-linear-layout – olyv