1、代码:
package cn.zengfansheng.manualUI;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WindowManager wm = (WindowManager) this.getSystemService(WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth() / 3;// 获取屏幕宽度/3
LinearLayout llLayout = new LinearLayout(this);
llLayout.setOrientation(LinearLayout.VERTICAL);// 设置线性布局的朝向
TextView tv1 = new TextView(this);
tv1.setText("内容1");
tv1.setBackgroundColor(Color.GREEN);
llLayout.addView(tv1, width,LayoutParams.WRAP_CONTENT);
TextView tv2 = new TextView(this);
tv2.setText("内容2");
tv2.setBackgroundColor(Color.GREEN);
llLayout.addView(tv2, width,LayoutParams.WRAP_CONTENT);
this.setContentView(llLayout);
}
} |