zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android常用控件

Android 常用 控件
2023-06-13 09:12:01 时间

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

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.cn/155318.html原文链接:https://javaforall.cn