Android常用控件
大家好,又见面了,我是你们的朋友全栈君。
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
相关文章
- android onresume方法,非静态方法’onResume’Android Studio
- 代码加密 android,Android 开发怎样做代码加密或混淆「建议收藏」
- android activity自动跳转,Android activity跳转之finish()方法的作用
- android签名命令行,Android系统签名位置及命令
- strictmode android,Android中的StrictMode
- android之常用控件大集合
- Android最常用的控件ListView(详解)
- android短信验证码代码,Android短信验证码自动填写实现代码
- Android angle_android 界面悬停
- Android preference_android studio preview
- Android触摸事件_android设置按钮点击事件
- CA周记 - 跟我做⼀个高德地图的 iOS / Android MAUI 控件(前言)
- 【Android RTMP】音频数据采集编码 ( AAC 音频格式解析 | FLV 音频数据标签解析 | AAC 音频数据标签头 | 音频解码配置信息 )
- 【Android 安全】DEX 加密 ( 常用 Android 反编译工具 | apktool | dex2jar | enjarify | jd-gui | jadx )
- 【错误记录】Android Studio 编译报错 ( Could not find com.android.tools.build:gradle:4.2.1. )
- 【Android 逆向】Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )
- 【Android Gradle 插件】主工程与依赖库变体配置 (LibraryExtension#libraryVariants|AppExtension#applicationVariants)
- android sdutio常用快捷键详解手机开发
- Android 深入ViewPager补间动画,实现类京东商城首页广告Banner切换效果详解手机开发
- android异步任务asyntask详解手机开发
- Android通过HttpClient执行post请求详解编程语言
- Android常用单位转换的辅助类详解编程语言
- 谷歌的Google Assistant会成为下一个Android吗?| CES 2019
- 解析Android资源文件及他们的读取方法详解