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. Android检查更新(是否强制更新)

    Android应用客户端通常会需要更新,而且根据需求分为普通更新和强制更新.是否强制更新可通过检查更新时从服务器获取的标志位来判断. public class UpdateManager { priv ...

  2. 2018.8.1 Java中的反射和同步详解

    为何要使用同步? java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数据的增删改查), 将会导致数据不准确,相互之间产生冲突,因此加入同步锁以避免在该线程没有完成操作之前,被其他 ...

  3. 2017.11.17 C++系列---用malloc动态给c++二维数组的申请与释放操作

    方法一:利用二级指针申请一个二维数组. #include<stdio.h> #include<stdlib.h> int main() { int **a; //用二级指针动态 ...

  4. 2017.9.24 JSP动态页面

    1.1 JSP(Java Server Page)是一种运行在服务器端的脚本语言,用来开发动态网页的开发技术. 1.2 JSP页面的结构 JSP页面主要由HTML和JSP代码构成,JSP代码是通过&q ...

  5. 在VS中使用Boost库出现Macro redefinition错误的解决方法(warning C4005)

    最近使用Boost库做多线程开发,可视在vs中编译工程师总是遇到Macro redefinition错误,类似下面的错误描述 1>c:\program files (x86)\microsoft ...

  6. AngularJS 外部文件中的控制器其他实例

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  7. Python线程创建与使用

    Python多为线程编程提供了两个简单明了的模块:thread和threading,Python3中已经不存thread模块,已经被改名为_thread,实际优先使用 threading模块. 1.P ...

  8. 浅谈MySQL字符集

      Preface       MySQL use character set & collation to organize the different charater.It provid ...

  9. matlab2018a安装后帮助文档打不开解决方法

    安装matlab2018a破解版后,帮助文档提示需要许可证问题(破解版没有可用许可证): 解决方法是把文档设置为离线即可(预设---->帮助---->安装在本地---->小窗口)

  10. uplift model学习笔记

    一.解决的问题: 通常的 Propensity Model 和 Response Model 只是给目标用户打了个分,并没有确保模型的结果可以使得活动的提升最大化:它没有告诉市场营销人员,哪个用户最有 ...