Android常用控件

Android常用控件TextView显示文本<TextViewandroid:id="@+id/text_view"android:layout_width="match_pa

大家好,又见面了,我是你们的朋友全栈君。

TextView

显示文本

<TextView
	android:id="@+id/text_view"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:gravity="center"
	android:textSize="24sp"
	android:textColor="#00ff00"
	android:text="This is TextView" />

android:id :给当前控件定义了一个唯一标识符。

android:layout_width:指定控件的宽度,可选值:match_parent (fill_parent)和wrap_content ,match_parent 表示让当前的控件的大小和父布局的大小一样,也就是由父布局来决定当前控件的大小;wrap_content表示让当前控件的大小能够刚好包含住里面的内容,也就是由控件内容决定当前大小,也可以设置特定的大小。

android:layout_height:指定控件的高度,内容同上。

android:gravity :来指定文字的对齐方式,可选值有top、bottom、left、right、center等。

android:textSize: 指定文字的大小。

android:textColor: 指定文字的颜色。

android:text:指定TextView中的文本显示内容。

Button

最常用的按钮

<Button
	android:id="@+id/button"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:text="Button"
	android:textAllCaps="false" />

android:textAllCaps:是否将英文字母自动转换成大写

EditText

在控件里输入和编辑内容

<EditText
    android:id="@+id/edit_text"
    android:layout_width="match_parent"
    android:Layout_height="wrap_content"
    android:hint="Type something here"
    android:maxLines="2"/>

android:hint :指定了一段提示性的文本。

android:maxLines :指定了EditText的最大行数为两行,这样当输入的内容超过两行时,文本就会向上滚动,而EditText则不会再继续拉伸。

ImageView

在界面上展示图片,图片通常都是放在以“drawable”开头的目录下。

<ImageView
	android:id="@+id/image_view"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:src="@drawable/img"
/>

android:src :给ImageView指定了一张图片。

ProgressBar

用于在界面上显示一个进度条,表示我们的程序正在加载一些数据。

<ProgressBar
	android:id="@+id/progress_bar"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	style="?android:attr/progressBarStyleHorizontal"
	android:max="100"
/>

style:设置进度条样式。

android:max:设置进度条最大值。

AlertDialog

可以在当前的界面弹出一个对话框,置顶于所有界面元素之上的,能屏蔽掉其他控件的交互能力。

@Override
public void onClick(View v) {
	switch (v.getId()) {
		case R.id.button:
			AlertDialog.Builder dialog = new AlertDialog.Builder (MainActivity.this);
			dialog.setTitle("This is Dialog");
			dialog.setMessage("Something important.");
			dialog.setCancelable(false);
			dialog.setPositiveButton("OK", new DialogInterface.
				OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			dialog.setNegativeButton("Cancel", new DialogInterface.
				OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			dialog.show();
			break;
		default:
			break;
		}
	}
}

ProgressDialog

类似于AlertDialog,会在对话框中显示一个进度条。

@Override
public void onClick(View v) {
	switch (v.getId()) {
		case R.id.button:
			ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
			progressDialog.setTitle("This is ProgressDialog");
			progressDialog.setMessage("Loading...");
			progressDialog.setCancelable(true);
			progressDialog.show();
			break;
		default:
			break;
		}
	}
}

注意,如果在setCancelable() 中传入了false ,表示ProgressDialog是不能通过Back键取消掉的,这时你就一定要在代码中做好控制,当数据加载完成后必须要调用ProgressDialog的dismiss() 方法来关闭对话框,否则ProgressDialog将会一直存在。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/155318.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 2D网络游戏(2d网络游戏大全列表)

    2D网络游戏开发(网络篇)(一) 作者:akinggw “2D网游开发”,我有时都觉得这个标题包含的内容太多,要实现起来也太难。于是,我决定将它分门别类,我按照我要实现的一个网络游戏将它分成下面几个部分: l      客户端l      网络端l      服务器端l      网页端 我们来讲解一下,我们分别要在每个端口完成什么内容: 

    2022年4月16日
  • python语言中变量的命名规则是什么_Python中变量的命名规则

    python语言中变量的命名规则是什么_Python中变量的命名规则讲解对象:Python中变量的命名规则作者:融水公子rsgz➢>变量的命名理解Python需要使用标识符给变量命名,其实标识符就是用于给程序中变量、类、方法命名的符号(简单来说,标识符就是合法的名字)。➢>命名要求Pvthon语言的标识符必须以字母、下画线()开头,后面可以跟任意数目的字母、数字和下画线➢>注意此处的字母并不局限于26个英文字母可以包含中文字符、日文字符等…

    2022年5月4日
  • UART和USART 有区别

    UART和USART 有区别UART:universalasynchronousreceiverandtransmitter通用异步收发器          [BusSignal]  TX ,RX USART:universalsynchronousasynchronousreceiverandtransmitter通用同步异步收发器          [BusSi

    2022年5月19日
  • Java 十六进制(Hex)与byte数组之间的转换

    Java 十六进制(Hex)与byte数组之间的转换

    2022年2月10日
  • PAT考试总结(考试心得)

    pat试题总结遍历问题的总结dfs中,如果是有环的图,要设置visited数组防止绕圈,同时在dfs函数退出前要将visited数组相应设置为false,否则其他路径就不能遍历该结点;在问题中,如果要求“从一个序列中选取若干个元素来满足条件”,可以考虑dfs,如1103IntegerFactorization(30分)和7-1Forever(20分);字符串处理总结字符串处理中,注意利用sscanf,可以按照格式读取字符串中的数字,如sscanf(s,“Therootis

    2022年4月15日
  • Ios frida_frida

    Ios frida_frida一、FridaFrida是一款基于Python+javascript的hook框架,通杀android\iOS\linux\win\osx各平台。Frida原理是手机端安装一个server程序把手机端的端口转到PC端写的python脚本进行通信,而Python脚本中采用javascript语言编写hook代码。①installfridaondeviceStartCydiaandaddFrida’srepositorybynavig

    2022年10月30日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号