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

可选值有visibleinvisiblegone其默认值为visibilegone表示控件不可见且不占用任何屏幕空间。可以通过代码控制可见性,使用setVisibility()方法,可以传入View.VISIBLEView.INVISIBLEView.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种基本布局的更多相关文章

  1. 【Android Studio】安卓开发初体验3.1——UI设计之常用控件

    常用控件 首先对xml文件的编辑有三种模式 Code为纯代码 Split是一边代码,一边预览效果图 Designer就是有UI设计界面 TextView 用于在界面上显示一段文本信息 所有控件都可以在 ...

  2. Windows 8.1 应用再出发 - 几种常用控件

    本篇为大家简单介绍Windows 商店应用中控件的用法,为方便讲解,我们在文本控件和按钮控件这两类中分别挑选有代表性的控件进行详细说明. 1. 文本控件 (1) TextBlock TextBlock ...

  3. WPF 几种常用控件样式的总结

    这里把wpf中几种常用样式总结一下,后期可以直接拷贝使用,呵呵 一.Button <ResourceDictionary xmlns="http://schemas.microsoft ...

  4. wpf 几种常用控件样式

    转自:http://blog.csdn.net/xuejiren/article/details/39449515

  5. [WinForm]WinForm跨线程UI操作常用控件类大全

    前言 在C#开发的WinForm窗体程序开发的时候,经常会使用多线程处理一些比较耗时之类的操作.不过会有一个问题:就是涉及到跨线程操作UI元素. 相信才开始接触的人一定会遇上这个问题. 为了解决这个问 ...

  6. UI常用控件

    UICommonlyUsedControls [UI常用控件] 不需要学习多么深入,但是要知道系统提供的有用的控件. 一.UISwitch(开关) 二.UIActivityIndicatorView( ...

  7. Xamarin Studio在Mac环境下的配置和Xamarin.iOS常用控件的示例

    看过好多帖子都是Win环境装XS,Mac只是个模拟器,讲解在Mac环境下如何配置Xamarin Studio很少,也是一点点找资料,东拼西凑才把Xamarin Studio装在Mac上跑起来,如下: ...

  8. Android笔记---常用控件以及用法

    这篇文章主要记录下Android的常用控件以及使用的方法,Android 给我们提供了大量的UI控件,合理地使用这些控件就可以非常轻松地编写出相当不错的界面,这些是Android学习的基础,没有什么业 ...

  9. MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)

    本节继续讲解常用控件--按钮控件的使用. 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box)等.命令按钮就是我们前面多次提到的侠义的 ...

随机推荐

  1. 【转】VMware虚拟机系统无法上网怎么办?

    有很多用户通过安装VMware软件来创建虚拟机系统,其中就有部分用户在创建好虚拟机系统后遇到无法上网的问题,下面PC6苹果网小编就给大家带来VMware虚拟机系统下无法上网的解决办法: 1.在虚拟机右 ...

  2. php中的脚本加速扩展opcache

    今儿在azure里装php5.5.4,发现原先php5.4.php5.3中的zend guard laoder以及php5.2中的Zend Optimizer均不能再用,一直很喜欢用的eacceler ...

  3. office2010

    MS office2010 360网盘:http://yunpan.cn/QajXaRWbnpTzF (提取码:cf72) 如何激活参见我下面的博客: http://www.cnblogs.com/l ...

  4. Git基础篇

    对于Git的一些基础了解,安装,里面的一些名词,这里就不做介绍了.主要记录怎么使用GIt. 本篇介绍: 配置个人信息        生成本地仓库并与远程库相连        添加SSH秘钥       ...

  5. Python——字典

    字典是一种key-value 的 数据类型,使用就想我们上学用的字典.可以通过笔画,字母来查对应页的详细内容. 特性:1. 字典是无须的.(如果光打印字典里的字符串,那么排序不会按照顺序排,因为字典是 ...

  6. MySql错误1045 Access denied for user 'root'@'localhost' (using password:YES)

    1.先停止mysql服务 2.进入mysql的安装路径,找到并打开my.ini文件,找到[mysqld],在该行下面添加 skip_grant_tables,也就是通知mysql,在登陆的时候跳过密码 ...

  7. 利用Selenium+java实现淘宝自动结算购物车商品(附源代码)

    转载请声明原文地址! 本次的主题是利用selenium+java实现结算购买购物车中的商品. 话不多说,本次首先要注意的是谷歌浏览器的版本,浏览器使用的驱动版本,selenium的jar包版本.   ...

  8. nginx+php-fpm结构模型剖析及优化(转载)

    一.nginx和php-fpm的关系和分工 nginx是web服务器,php-fpm是一个PHPFastCGI进程管理器,两者遵循fastcgi的协议进行通信,nginx负责静态类似html文件的处理 ...

  9. 屏蔽datatable错误提示

    $.fn.dataTable.ext.errMode = 'none'; //不显示任何错误信息// 以下为发生错误时的事件处理,如不处理,可不管.$('#productionRequestItems ...

  10. rsync + git发布项目

    前言: 更新项目的时候需要将更改的文件一一上传,这样比较麻烦,用版本控制器git +rsync 搭建一个发布服务器,以后发布文件非常方便 首先说下,我这边的更新流程,本地写完之后,git push 到 ...