Android 动态布局 (代码布局)

package com.example.testframelayout;



import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

import android.view.Menu;
import android.view.ViewGroup;

import android.widget.TextView;

public class MainActivity extends Activity {

	private LinearLayout root;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		root = new LinearLayout(this);

		simpleAddView();
		setContentView(root);

	}

	private void simpleAddView() {// 简单的用代码添加布局
		LinearLayout linlayout = new LinearLayout(this);
		LinearLayout.LayoutParams linLayoutParams = new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		linlayout.setBackgroundColor(Color.BLUE);
		TextView textView = new TextView(this);
		textView.setText("测试");
		linlayout.addView(textView);
		root.addView(linlayout, linLayoutParams);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}


你可能感兴趣的:(android)