Day3 UI:7种常用控件、4种基本布局
Android常用控件
TextView
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" //top、bottom、left、right、center等 可以用'|'来同时指定多个值
android:textSize="24sp" //文字大小
android:textColor="#00ff00" //文字颜色
android:text="This is TextView"/>
更多细节可以查阅官方文档
https://developer.android.com/reference/android/widget/TextView.html
Button
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:textAllCaps="false" //禁用按钮的默认大写功能/>
在Activity中为Button的点击注册一个监听器:
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//在此处添加逻辑
}
});
EditText
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something here" //提示文字
/>
Button与EditText完成一些功能,在Activity中添加:
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.edit_text);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String inputText = editText.getText().toString(); //读取字符串
Toast.makeText(MainActivity.this, inputText, Toast.LENGTH_SHORT).show(); //输出字符串
ImageView
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img_1"
/>
通过在Activity中动态修改图片:
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.image_view);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
imageView.setImageResource(R.drawable.img_2); //更改图片的src
}
});
}
ProgressBar
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
所有的Android控件都有一个属性用于设置可见性android:visibility
可选值有visible、invisible、gone其默认值为visibile,gone表示控件不可见且不占用任何屏幕空间。可以通过代码控制可见性,使用setVisibility()方法,可以传入View.VISIBLE、View.INVISIBLE、View.GONE这3种值。
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar)findViewById(R.id.progress_bar);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(progressBar.getVisibility() == View.GONE) //用getVisibility()方法获取可见性
progressBar.setVisibility(View.VISIBLE);
else
progressBar.setVisibility(View.GONE);
}
});
}
改为水平进度条:在xml中添加
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
在代码中控制进度条
@Override
public void onClick(View v) {
int progress=progressBar.getProgress();
progress=progress+10;
progressBar.setProgress(progress);
AlertDialog
AlertDialog和ProgressDialog都可以屏蔽掉其他控件的交互能力
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//通过AlertDialog.Builder创建一个AlertDialog实例
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
//设置标题
dialog.setTitle("Oh!");
//设置内容
dialog.setMessage("Are u fucking sure you want to do this?");
//可否取消(即可否不操作就退出)
dialog.setCancelable(false);
//设置按钮
dialog.setPositiveButton("Sure", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
//设置取消按钮
dialog.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
}
});
}
ProgressDialog
@Override
public void onClick(View v) {
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("This is ProgressDialog");
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.show();
}
详解四种基本布局
线性布局LinearLayout
设置控件水平排列还是垂直排列:
android:orientation="vertical" //vertical是垂直排列,horizontal是水平排列
将宽度设置为0dp,用weight来指定所占比例(dp是指定控件大小、间距等属性的单位)
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
仅指定EditText的weight属性,并将Button的宽度改回wrap_content会使适配方面会非常好,而且看起来也更加舒服。
<EditText
android:id="@+id/input_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type something"
/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
/>
相对布局RelativeLayout
RelativeLayout可以通过相对定位的方式让控件出现在布局的任何位置,因此,RelativeLayout中的属性非常多,不过这些属性都是有规律可循的,其实并不难理解和记忆。
<Button
android:layout_centerInParent="true" //居中
android:layout_alignParentLeft="true" //居左
android:layout_alignParentRight="true" //居右
android:layout_alignParentTop="true" //居上
android:layout_alignParentBottom="true" //局下
/>
...
上面例子中都是相对于父布局进行定位的,相对于控件定位:
<Button
android:layout_above="@id/button3" //位于b3的上方
android:layout_toLeftOf="@id/button3"
android:layout_below="@id/button3"
android:layout_toRightOf="@id/button3"
android:layout_alignRight="@id/button3" //位于b3的右边
android:layout_alignLeft="@id/button3"
android:layout_alignTop="@id/button3"
android:layout_alignBottom="@id/button3"
/>
...
需要注意的是,当一个控件去引用另一个控件的id时,该控件一定要定义在引用空间的后面,不然会出现找不到id的情况。
帧布局FrameLayout
该布局所有控件都会默认摆放在布局的左上角,后定义的控件会放在先定义的上方,也可以使用像LinearLayout中的android:layout_gravity属性来指定控件的对其方式。
由于FrameLayout定位方式的欠缺,导致它的应用场景也比较少,下一章中介绍碎片的时候我们还是可以用到它的。
百分比布局PercentFrameLayout/PercentRelativeLayout
由于只有LinearLayout支持使用layout_weight属性来实现比例控制大小的功能,其他两种布局都不支持,才引入了百分比布局。
使用前要在build.gradle中添加百分比布局库的依赖
打开app/build.gradle文件,在dependencies闭包中添加:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
//添加这一行↓
compile 'com.android.support:percent:24.2.1'
testCompile 'junit:junit:4.12'
}
然后点击Sync Now即可把新添加的百分比布局库引入到项目当中。
<android.support.percent.PercentFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:text="Button 1"
android:layout_gravity="left|top"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
/>
</android.support.percent.PercentFrameLayout>
PercentFrameLayout继承了FrameLayout的特性,所以要用layout_gravity设置位置。
PercentRelativeLayout的用法也是非常相似的,它继承了RelativeLayout中的所有属性,并且可以设置宽高比例,其实Android还有AbsoluteLayout、TableLayout等布局,但使用得实在是太少了,就不讲解了。
Day3 UI:7种常用控件、4种基本布局的更多相关文章
- 【Android Studio】安卓开发初体验3.1——UI设计之常用控件
常用控件 首先对xml文件的编辑有三种模式 Code为纯代码 Split是一边代码,一边预览效果图 Designer就是有UI设计界面 TextView 用于在界面上显示一段文本信息 所有控件都可以在 ...
- Windows 8.1 应用再出发 - 几种常用控件
本篇为大家简单介绍Windows 商店应用中控件的用法,为方便讲解,我们在文本控件和按钮控件这两类中分别挑选有代表性的控件进行详细说明. 1. 文本控件 (1) TextBlock TextBlock ...
- WPF 几种常用控件样式的总结
这里把wpf中几种常用样式总结一下,后期可以直接拷贝使用,呵呵 一.Button <ResourceDictionary xmlns="http://schemas.microsoft ...
- wpf 几种常用控件样式
转自:http://blog.csdn.net/xuejiren/article/details/39449515
- [WinForm]WinForm跨线程UI操作常用控件类大全
前言 在C#开发的WinForm窗体程序开发的时候,经常会使用多线程处理一些比较耗时之类的操作.不过会有一个问题:就是涉及到跨线程操作UI元素. 相信才开始接触的人一定会遇上这个问题. 为了解决这个问 ...
- UI常用控件
UICommonlyUsedControls [UI常用控件] 不需要学习多么深入,但是要知道系统提供的有用的控件. 一.UISwitch(开关) 二.UIActivityIndicatorView( ...
- Xamarin Studio在Mac环境下的配置和Xamarin.iOS常用控件的示例
看过好多帖子都是Win环境装XS,Mac只是个模拟器,讲解在Mac环境下如何配置Xamarin Studio很少,也是一点点找资料,东拼西凑才把Xamarin Studio装在Mac上跑起来,如下: ...
- Android笔记---常用控件以及用法
这篇文章主要记录下Android的常用控件以及使用的方法,Android 给我们提供了大量的UI控件,合理地使用这些控件就可以非常轻松地编写出相当不错的界面,这些是Android学习的基础,没有什么业 ...
- MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)
本节继续讲解常用控件--按钮控件的使用. 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box)等.命令按钮就是我们前面多次提到的侠义的 ...
随机推荐
- 1.6 NBU Catalog备份还原
用户的数据保存到了磁盘或者磁带中,并且是安全的,NBU所在的机器还有可能发生故障,需要重新安装或者将NBU部署到其他的机器中继续使用. 在这种情况下,如何让NBU知道用户已经存在的备份策略和存储单元配 ...
- Ajax的学习笔记(一)
AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),ajax并不是一门单独的语言,而是一种技术,是指一种创建交互式网页应用的网页开发技术. ...
- 3.Netty的粘包、拆包(二)
Netty提供的TCP数据拆包.粘包解决方案 1.前言 关于TCP的数据拆包.粘包的介绍,我在上一篇文章里面已经有过介绍. 想要了解一下的,请点击这里 Chick Here! 今天我们要讲解的是Net ...
- Hive[6] HiveQL 查询
6.1 SELECT ... FROM 语句 hive> SELECT name,salary FROM employees; --普通查询 hive>SELECT e.n ...
- Go Doc文档
Go为我们提供了快速生成文档和查看文档的工具,很容易编写查看代码文档.在项目协作过程中,可以帮助我们快速理解代码. 查看文档方式有两种:一种是通过终端查看,使用go doc命令,一种是通过网页查看,使 ...
- Centos7上搭建activemq集群和zookeeper集群
Zookeeper集群的搭建 1.环境准备 Zookeeper版本:3.4.10. 三台服务器: IP 端口 通信端口 10.233.17.6 2181 2888,3888 10.233.17.7 2 ...
- 【转载】CentOS7.0下安装Telnet
1..先检查CentOS7.0是否已经安装以下两个安装包:telnet-server.xinetd.命令如下: # rpm -qa telnet-server # rpm -qa xinetd 如果没 ...
- ln -s 软链接产生Too many levels of symbolic links错误
不能使用相对路径, ln -s ./cmake /usr/bin/ 而是要 ln -s /usr/local/bin/cmake /usr/bin/
- 裸机——ADC
1.首先是ADC的基本知识 模拟信号,连续的 数字信号,离散的 模拟信号,现实世界的很多东西都是连续的,所以使用模拟信号才能准确描述,但是模拟信号不方便控制. 数字信号,计算机中的信号大都为数字的,数 ...
- Robots Gym - 101915G
传送门 The Robotics Olympiad teams were competing in a contest. There was a tree drawn on the floor, co ...