Android成长之路-编码实现软件界面
2024-08-26 16:17:56
实现一个登陆界面:
相对布局:
- package cn.csdn.codeui;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- public class LoginRelativeActivity extends Activity {
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- initUI();
- }
- private void initUI() {
- RelativeLayout rlayout = new RelativeLayout(this);
- int id = 100;
- /**添加一个TextView*/
- TextView textView1 = new TextView(this);
- /**android:id=""*/
- textView1.setId(id);
- /**android:text="用户名:"*/
- textView1.setText("用户名:");
- /**android:layout_width="wrap_content"
- android:layout_height="wrap_content"*/
- RelativeLayout.LayoutParams textParams1 = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- /**android:layout_alignParentLeft="true"*/
- textParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
- rlayout.addView(textView1, textParams1);
- //////////
- int id1 = 200;
- EditText userEdit = new EditText(this);
- userEdit.setId(id1);
- RelativeLayout.LayoutParams EditParams1 = new RelativeLayout.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
- /**android:layout_toRightOf="id的值"*/
- EditParams1.addRule(RelativeLayout.RIGHT_OF, id);
- rlayout.addView(userEdit, EditParams1);
- //////////
- int Id = 300;
- TextView textView2 = new TextView(this);
- textView2.setId(Id);
- textView2.setText("密码 :");
- RelativeLayout.LayoutParams TextParams2 = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- TextParams2.addRule(RelativeLayout.BELOW, id1);
- rlayout.addView(textView2, TextParams2);
- //////////
- int Id1 = 400;
- EditText passEdit = new EditText(this);
- passEdit.setId(Id1);
- RelativeLayout.LayoutParams EditParams2 = new RelativeLayout.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
- EditParams2.addRule(RelativeLayout.BELOW, id1);
- EditParams2.addRule(RelativeLayout.RIGHT_OF, Id);
- rlayout.addView(passEdit, EditParams2);
- //////////
- int Id2 = 500;
- Button login = new Button(this);
- login.setId(Id2);
- login.setText("登陆");
- RelativeLayout.LayoutParams loginParams = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- loginParams.addRule(RelativeLayout.BELOW, Id1);
- loginParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
- rlayout.addView(login, loginParams);
- //////////
- Button insert = new Button(this);
- insert.setText("注册");
- RelativeLayout.LayoutParams insertParams = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- insertParams.addRule(RelativeLayout.BELOW, Id1);
- insertParams.addRule(RelativeLayout.LEFT_OF, Id2);
- rlayout.addView(insert, insertParams);
- setContentView(rlayout);
- }
- }
效果图:
表格布局:
- package cn.csdn.codeui;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TableLayout;
- import android.widget.TableRow;
- import android.widget.TextView;
- public class LoginTableActivity extends Activity {
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- initUI();
- }
- private void initUI() {
- ///表格布局
- TableLayout tlayout = new TableLayout(this);
- tlayout.setColumnStretchable(1, true);
- ///行
- TableRow tableRow1 = new TableRow(this);
- TextView textView1 = new TextView(this);
- textView1.setText("用户名:");
- tableRow1.addView(textView1);
- EditText userEdit = new EditText(this);
- tableRow1.addView(userEdit);
- tlayout.addView(tableRow1);
- TableRow tableRow2 = new TableRow(this);
- TextView textView2 = new TextView(this);
- textView2.setText("密码:");
- tableRow2.addView(textView2);
- EditText passEdit = new EditText(this);
- tableRow2.addView(passEdit);
- tlayout.addView(tableRow2);
- TableRow tableRow3 = new TableRow(this);
- Button btn0 = new Button(this);
- btn0.setText("登录");
- tableRow3.addView(btn0);
- Button btn1 = new Button(this);
- btn1.setText("注册");
- tableRow3.addView(btn1);
- tlayout.addView(tableRow3);
- setContentView(tlayout);
- }
- }
效果图:
线性布局:
- package cn.csdn.codeui;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- public class LoginLinearActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- init();
- }
- private void init() {
- //线性布局
- LinearLayout linearLayout = new LinearLayout(this);
- /**android:orientation="vertical"*/
- linearLayout.setOrientation(LinearLayout.VERTICAL);
- LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
- LayoutParams.FILL_PARENT);
- //////////
- TextView userText = new TextView(this);
- userText.setText("用户名:");
- LayoutParams userTextParams = new LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
- linearLayout.addView(userText, userTextParams);
- //////////
- EditText userEdit = new EditText(this);
- LayoutParams userEditParams = new LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
- linearLayout.addView(userEdit, userEditParams);
- //////////
- TextView passText = new TextView(this);
- passText.setText("密码:");
- LayoutParams passTextParams = new LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
- linearLayout.addView(passText, passTextParams);
- //////////
- EditText passEdit = new EditText(this);
- LayoutParams passEditParams = new LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
- linearLayout.addView(passEdit, passEditParams);
- //////////
- Button login = new Button(this);
- login.setText("登陆");
- LayoutParams loginParams = new LayoutParams(LayoutParams.FILL_PARENT,
- LayoutParams.WRAP_CONTENT);
- linearLayout.addView(login, loginParams);
- //////////
- Button insert = new Button(this);
- insert.setText("注册");
- LayoutParams insertParams = new LayoutParams(LayoutParams.FILL_PARENT,
- LayoutParams.WRAP_CONTENT);
- linearLayout.addView(insert, insertParams);
- setContentView(linearLayout, layoutParams);
- }
- }
效果图:
最新文章
- codevs 2287 火车站
- WinCE项目应用之RM905a+医用放射性核素活度计
- eclipse使用tips-Toggle Mark Occurrences 颜色更改
- 【转载】 Pyqt 利用QDataStream对文件进行存取
- (笔记)angular 包含关系的controller参数传递
- 相对布局RelativeLayout
- Asp.net Json 解析 与 直接用ip访问返回josn
- bat 批处理脚本
- MVC-@html.ActionLink的几种参数格式
- 地址重写--Java中urlrewriter的使用
- java基础系列--Date类
- MySQL grant命令使用
- 前端自动化测试神器-Katalon进阶用法
- nginx,maven
- 关于ico图标
- Hibernate中的实体规则、对象状态和进阶-一级缓存
- 静态和实例方法区别-java
- Concurrency in C# Cookbook 笔记
- php7 安装mssql 扩展
- Spring Cloud 各组件调优参数
热门文章
- apt-get和apt-cache命令实例展示
- Java并发教程(Oracle官方资料)
- 仿LOL项目开发第二天
- mvn sonar:sonar在jenkins步骤的执行位置影响执行结果
- WEB渗透之对于开了3389远程连接不上的总结
- iFrame的妙用
- Microsoft Visual C++ 14.0 is required问题的解决或warning: Debugger speedups using cython not found
- RHEL7.0 配置网络IP的三种方法
- 将项目的版本控制从Vss迁移到Git
- 自定义错误信息并写入到Elmah