Android用户界面基础之使用编码方式绘制登录界面

实例:

这里写图片描述

package com.ecainiao.code.drawlogin;

import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {
    private LinearLayout rot,row1,row2,row3;
    private TextView tvName,tvPassword;
    private EditText etName,etPassword;
    private Button btnLogin,btnRegister;
    //设置宽度和高度,权重的参数
    private LayoutParams params;
    public void setupView(){
        //实例化控件和容器
        params = new LayoutParams(0,LayoutParams.WRAP_CONTENT,1.0f);
        rot = new LinearLayout(this);
        rot.setOrientation(LinearLayout.VERTICAL);
        row1 = new LinearLayout(this);
        row1.setOrientation(LinearLayout.HORIZONTAL);
        row2 = new LinearLayout(this);
        row2.setOrientation(LinearLayout.HORIZONTAL);
        row3 = new LinearLayout(this);
        row3.setOrientation(LinearLayout.HORIZONTAL);

        tvName = new TextView(this);
        tvName.setText("用户名");
        tvPassword = new TextView(this);
        tvPassword.setText("密    码");

        etName = new EditText(this);
        etName.setLayoutParams(params);
        etPassword = new EditText(this);
        etPassword.setLayoutParams(params);

        btnLogin = new Button(this);
        btnLogin.setText("注册");
        btnRegister = new Button(this);
        btnRegister.setText("登录");

        //组织视图树
        row1.addView(tvName);
        row1.addView(etName);

        row2.addView(tvPassword);
        row2.addView(etPassword);

        row3.addView(btnLogin,params);
        row3.addView(btnRegister,params);

        rot.addView(row1);
        rot.addView(row2);
        rot.addView(row3);

        //添加视图树到Activity区
        setContentView(rot);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupView();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值