[android] 03_纯手工创建UI

Android 4.0

纯手工创建UI

特殊应用场景——手工编写可以精简R.java文件输入法的界面
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);
    }
}
2、结果: