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. django中的forms组件(权限信息校验,增删改查)

    1.用处 1.用户请求数据验证 2.自动生成错误信息 3.打包用户提交的正确信息 4.如果其中有一个错误了,其他的正确,则保留上次输入的内容 5.自动创建input标签并可以设置样式 6.基于form ...

  2. java 基础词汇 必须 第九天

    Collection 集合 List 列表集合 Set 不重复集合 Linked 链表 Vector 线程安全集合 Hash 哈希值 tree 树型结构 Map 键值对集合 add 增加 remove ...

  3. Open closed principle

    #include <iostream> using namespace std; class Book { public: string getContents() { return &q ...

  4. js对象动态添加属性,方法

    1. 动态添加属性,方法 var object = new Object(); object.name = "name"; object.age = 19; >>> ...

  5. GNU汇编 存储器访问指令

    .text .global  _start _start: mov r0,#0xff str r0,[r1] ldr r2,[r1]

  6. 汇编:实现C语言的 ||与&&运算

    ;C程序转汇编(或运算链接) DATAS SEGMENT a Dw b dw cc dw d dw m dw n dw string db dup(?) DATAS ends CODES SEGMEN ...

  7. POJ:3045-Cow Acrobats

    Cow Acrobats Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6253 Accepted: 2345 Descript ...

  8. Immutable

    Immutable 参考文章 https://zhuanlan.zhihu.com/p/20295971?columnSlug=purerender

  9. java程序——随机数求和

    设计思路:用随机算法随机生成10个数(0~100),循环填充一个数组,然后在循环中显示数组内容,接着用一个循环计算数组元素的和,将结果也显示在消息框中. 流程图: 源代码: package test; ...

  10. 4.bootstrap的form表单的form-group和form-control的区别与联系

    1. form-group一般用于div form-control一般用于置于div中的标签元素,为了让控件在各种表单风格中样式不出错,需要添加类名“form-control”,如: <form ...