Android第一行代码——第三章控件和布局

本文详细介绍了Android中常用的控件如TextView、Button、EditText、ImageView等,以及LinearLayout、RelativeLayout等布局的使用方法。还涉及ListView和RecyclerView的实现步骤,包括适配器的编写,并提到了自定义控件和引入布局的技巧,最后讨论了编写界面的最佳实践。
摘要由CSDN通过智能技术生成
本章主要介绍常用的控件和基本的布局方法

- 常用控件

1. TextView
在界面上显示一段文本信息
 <TextView
        android:id="@+id/textView_Test"		//设置唯一标识符
        android:layout_width="wrap_content"		// 宽度
        android:layout_height="wrap_content"	//高度	
        android:gravity="center"				//文字对齐方式
        android:text="Hello World!"				//指定TextVIiew显示的内容
        android:textSize="24sp"					//指定文字大小	
        android:textColor="#00ff00"				//指定文字颜色
        />
2. Button
在界面上显示一个按钮
<Button
        android:id="@+id/button_Test"			//设置唯一标识符
        android:layout_width="match_parent"		
        android:layout_height="wrap_content"
        android:text="Hello"				//指定Button显示的内容
        android:textAllCaps="false"			//指定英文是否大写,默认是大写
        />
3.EditText
可编辑的文本,允许用户在控件里输入和编辑
<EditText
        android:id="@+id/EditText_Test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Type something here"		//显示提示性文字
        android:maxLines="2"/>					//指定最大行数
4.ImageView
在界面上显示图片
<ImageView
        android:id="@+id/imageView_Test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ccc"/>				//为ImaheVIew指定图片
5.ProgressBar
在界面上显示一个进度条
<ProgressBar
        android:id="@+id/progressBar_Test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"  //默认是圆形进度条,这个属性可改为水平进度条
        android:max="100"		//给进度条设置最大值
        />
6.AlertDialog
在当前界面弹出一个对话框,不同于上面的,这个是在程序中写的
	AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
    dialog.setTitle("This is a dialog");		//设置标题
    dialog.setMessage("Message");				//设置内容
    dialog.setCancelable(false);        //设置不可用Back键关闭对话框
   	//设置确定按钮的点击事件
    dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
   
     	@Override
     	public void onClick(DialogInterface dialogInterface, int i) {
   

         }
    });
    //设置取消按钮的点击事件
    dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
   
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
   

        }
    });
    dialog.shou();			
7.ProgressDialog
在当前界面弹出一个有进度条的对话框,也是在程序中完成
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setTitle("This is a ProgressDialog");
        progressDialog.setMessage("Loading...");
        progressDialog.setCancelable(false);
        progressDialog.show();

- 常用布局

1.LinearLayout
线性布局,将他所包含的控件在线性方向上依次排列
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"			//指定排列方式,水平|垂直
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button_Test"
        android:layout_width="0dp"				//使用比例控制大小时,由于排列是水平的,layout_width属性将不起作用,写成这样是一种规范
        android:layout_weight="1"				//使用比例来控制控件大小
        android:layout_gravity="center"			//指定对齐方式
        android:layout_height="wrap_content"
        android:text="Hello"
        android:textAllCaps="false"
        />
    <EditText
        android:id="@+id/EditText_Test"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:hint="Type something here"
        android:maxLines="2"/>

</LinearLayout>
2.RelativeLayout
相对布局,可通过相对于父布局或相对于其他控件来约束控件位置
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    //layout_alignParentLeft,layout_alignParentTop,layout_alignParentRight,
    //layout_alignParentButton,layout_centerInParent,相对于父布局
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Hello"
        android:textAllCaps="false"
        />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="Hello"
        android:textAllCaps="false"
        />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello"
        android:textAllCaps="false"
        />
 	 //  layout_toLeftOf,layout_toRightOf,layout_above,layout_below
  	//相对于其他布局   
    <Button
        android:id="@+id/button4"
        android:
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值