对于这个问题的将来的读者,这是一个更一般的答案。我们将进行的布局如下:
方法1:将TextView添加到现有的LinearLayout
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamic_linearlayout);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_example);
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
textView1.setText("programmatically created TextView1");
textView1.setBackgroundColor(0xff66ff66);
textView1.setPadding(20, 20, 20, 20);
linearLayout.addView(textView1);
TextView textView2 = new TextView(this);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.RIGHT;
layoutParams.setMargins(10, 10, 10, 10);
textView2.setLayoutParams(layoutParams);
textView2.setText("programmatically created TextView2");
textView2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
textView2.setBackgroundColor(0xffffdbdb);
linearLayout.addView(textView2);
}
请注意,LayoutParams
您必须指定导入的布局类型,如
import android.widget.LinearLayout.LayoutParams;
否则,您需要LinearLayout.LayoutParams
在代码中使用。
这是xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_example"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff99ccff"
android:orientation="vertical" >
</LinearLayout>
方法2:以编程方式创建LinearLayout和TextView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setBackgroundColor(0xff99ccff);
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
textView1.setText("programmatically created TextView1");
textView1.setBackgroundColor(0xff66ff66);
textView1.setPadding(20, 20, 20, 20);
linearLayout.addView(textView1);
TextView textView2 = new TextView(this);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.RIGHT;
layoutParams.setMargins(10, 10, 10, 10);
textView2.setLayoutParams(layoutParams);
textView2.setText("programmatically created TextView2");
textView2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
textView2.setBackgroundColor(0xffffdbdb);
linearLayout.addView(textView2);
setContentView(linearLayout);
}
方法3:以编程方式将一个xml布局添加到另一个xml布局
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamic_linearlayout);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dynamic_linearlayout_item, null);
FrameLayout container = (FrameLayout) findViewById(R.id.flContainer);
container.addView(view);
}
这是dynamic_linearlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/flContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
这是要添加的dynamic_linearlayout_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_example"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff99ccff"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff66ff66"
android:padding="20px"
android:text="programmatically created TextView1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffdbdb"
android:layout_gravity="right"
android:layout_margin="10px"
android:textSize="18sp"
android:text="programmatically created TextView2" />
</LinearLayout>