1.TextView

以下只是一部分属性,还有很多属性需要在用到时候再说

<TextView

android:textSize="24sp"//文字大小
android:textColor="#00ff00"//文字颜色
android:gravity="center"//排列方向
android:id="@+id/txtMainOne"
android:text="这是一个正规的活动界面"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

2.Button

<Button
android:id="@+id/btnMainOne"
android:text="按钮"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

在java代码中给按钮添加事件可以统一处理,方法是让类去实现接口View.OnClickListener

public class MainActivity extends Activity implements View.OnClickListener {

//在onCreate方法中绑定按钮的监听事件为类本身

Button btnMainOne=(Button)findViewById(R.id.btnMainOne);
btnMainOne.setOnClickListener(this);

//其他代码。。。。。。以下是对点击按钮的集中处理方式

@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnMainOne:
//编写逻辑
break;
default:
break;;
}
}

3.EditText

<EditText
android:id="@+id/txtMainTwo"
android:hint="起到提示信息的作用"
android:maxLines="2"//限制文本输入框只有2行,不会因为内容过多而出现控件无限拉长
android:layout_width="match_parent"
android:layout_height="wrap_content" />

//获取EditText 中的值

EditText txtMainTwo=(EditText)findViewById(R.id.txtMainTwo);
String value=txtMainTwo.getText().toString();
Toast.makeText(MainActivity.this,value,Toast.LENGTH_SHORT).show();

4.ImageView

<ImageView
android:id="@+id/imgMainOne"
android:src="@drawable/ic_launcher"//图片地址
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

ImageView imageView=(ImageView)findViewById(R.id.imgMainOne);
imageView.setImageResource(R.drawable.one);//修改图片

5.ProgressBar显示进度

<ProgressBar

android:visibility="visible"//所有空间都有这个熟悉,visible,invisible和gone,分别表示显示空间,隐藏控件但是占用位置,直接删除控件
android:id="@+id/pgbMainOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"//设置为横向表现
android:background="#ff5b7fff"//背景颜色
android:max="100"/>//最大值

代码控制显示

ProgressBar progressBar=(ProgressBar)findViewById(R.id.pgbMainOne);
if(progressBar.getVisibility()==View.GONE)
{
progressBar.setVisibility(View.VISIBLE);
}
else
{
progressBar.setVisibility(View.GONE);
}

//设置进度条

int progress=progressBar.getProgress();
progress+=10;
progressBar.setProgress(progress);

6.AlertDialog

AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("提示信息");
dialog.setMessage("很重要的信息");
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();

7.ProgressDialog

结合多线程展示

final ProgressDialog progressDialog=new ProgressDialog(MainActivity.this);
progressDialog.setTitle("提示");//设置标题
progressDialog.setCanceledOnTouchOutside(false);//是否在空白处点击返回主界面
progressDialog.setMax(100);//设置为横向进度条最大值
progressDialog.setMessage("提示内容信息");//提示内容
progressDialog.setCancelable(false);//是否选择取消键返回
progressDialog.setProgress(40);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置为横向进度
progressDialog.setIcon(R.drawable.one);//设置图标
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"确定",new DialogInterface.OnClickListener() {//添加按钮事件
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"按下按钮",Toast.LENGTH_LONG).show();
}
});
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});
progressDialog.show();//显示
new Thread(new Runnable() {//创建多线程
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
while (i < 100) {
try {
Thread.sleep(200);
// 更新进度条的进度,可以在子线程中更新进度条进度
progressDialog.incrementProgressBy(1);//设置进度条增加值
// dialog.incrementSecondaryProgressBy(10)//二级进度条更新方式
i++;

} catch (Exception e) {
// TODO: handle exception
}
}
progressDialog.dismiss();//在进度条走完时删除progressDialog

}
}).start();//启动多线程

8.布局:常用的4种布局LinearLayout,RelativeLayout,FrameLayout

LinearLayout:线性布局方式,android:orientation="vertical"表示竖向排列,android:orientation="horizontal"表示横向排列

android:layout_gravity="right"//设置控件的位置,当是横向的时候只能设置竖向的位置,当为竖向的时候只能设置横向的位置。

当设置android:orientation="horizontal"时可以设置控件的宽度是0:android:layout_width="0dp",然后设置宽度上的权重android:layout_weight="1"表示占用的比例

RelativeLayout:相对位置布局,可以设置对应的属性设置相对于父元素的坐标或者某个控件的坐标

相对于父元素:

layout_alignParentLeft

layout_alignParentTop

layout_alignParentRight

layout_centerInParent

layout_alignParentBottom

相对于某个控件:

android:layout_above="@+id/btnBThree"//相对于某个控件的上方

android:layout_below="@+id/btnBThree"//相对于某个空间的下方
android:layout_toLeftOf="@+id/btnBThree"
android:layout_toRightOf="@+id/btnBThree"
android:layout_toStartOf="@+id/btnBThree"
android:layout_toEndOf="@+id/btnBThree"

android:layout_alignBottom="@+id/btnBThree"
android:layout_alignTop="@+id/btnBThree"
android:layout_alignRight="@+id/btnBThree"
android:layout_alignLeft="@+id/btnBThree"
android:layout_alignEnd="@+id/btnBThree"
android:layout_alignStart="@+id/btnBThree"

FrameLayout:所有的控件都会在左上角定位,会出现重叠现象,不常用,但是在碎片的章节将会出现

TableLayout:允许以表格的方式排列控件,不常用。

9.自定义控件

先设计一个layout文件,然后再别的页面引入已经设计好的界面:<include layout="@layout/title"/>

如果是自定义控件:先定义一个类继承至LinearLayout,在构造函数中编写该类的事件

public class TitleLayout extends LinearLayout {

public TitleLayout(Context context,AttributeSet attrs)
{
super(context,attrs);
LayoutInflater.from(context).inflate(R.layout.title,this);

Button btnTitleOne=(Button)findViewById(R.id.title_back);
btnTitleOne.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
((Activity)getContext()).finish();//找到当前所在活动
}
});

Button btnTitleTwo=(Button)findViewById(R.id.title_edit);
btnTitleTwo.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getContext(),"你选择了编辑按钮",Toast.LENGTH_SHORT).show();
}
});
}
}

开始使用该用户控件

<com.example.zhb.test2.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"></com.example.zhb.test2.TitleLayout>

android入门到熟练(三)----UI界面的更多相关文章

  1. Android开发1:基本UI界面设计——布局和组件

    前言 啦啦啦~本学期要开始学习Android开发啦~ 博主在开始学习前是完完全全的小白,只有在平时完成老师要求的实验的过程中一步一步学习~从此篇博文起,博主将开始发布Android开发有关的博文,希望 ...

  2. Android研究之动态创建UI界面具体解释

     Android的基本UI界面一般都是在xml文件里定义好,然后通过activity的setContentView来显示在界面上.这是Android UI的最简单的构建方式.事实上,为了实现更加复 ...

  3. Android开发精彩博文收藏——UI界面类

    本文收集整理Android开发中关于UI界面的相关精华博文,共大家参考!本文不定期更新! 1. Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各 ...

  4. Android 子线程无法刷新UI界面

    问题:在Android开发中,子线程无法直接更改UI界面视图的刷新 这个时候 Handler 起到了至关重要的作用. 简单来说 , Handler就是用来传递消息的. Handler可以当成子线程与主 ...

  5. Android Studio 图形化设计 UI 界面

    我们开发 Android 程序必定是从 UI 开始的 ,使用最新版的 Android Studio 可以在图形化界面下设计软件 UI, Android Studio 默认的布局是 Constraint ...

  6. android入门到熟练(一)

    1.andro系统架构:Linux内核层(提供驱动),系统运行库层和android运行时库(提供C/C++库的主要特性,如SQLite,OpenGL,Webkit等和Dalvik虚拟机),应用框架层, ...

  7. Android入门(十):界面的布局方式及其实际应用

    关于Android界面布局,网上已经有了很多非常不错的学习资料,在这里我也不班门弄斧了,推荐两篇我认为写的不错的教程,然后再重点讲一下几种布局方式的实际应用. 教程链接:①http://www.cnb ...

  8. android入门到熟练(二)----活动

    1.活动创建对于每一个后端java类(继承至Activity或者ActionBarActivity)代码都有一个方法需要被重写[onCreate], 在此方法中可以加载界面资源文件或者绑定元素事件. ...

  9. Android Studio 屏幕方向以及UI界面状态的保存

    package com.example.orientation; import android.os.Bundle; import android.util.Log; import android.v ...

随机推荐

  1. JavaScript高级程序设计51.pdf

    (续上篇) 模拟鼠标事件 var btn=document.getElementById("myBtn"); //创建事件对象 var event=document.createE ...

  2. [SAM4N学习笔记]按键程序(查询方式)

    一.准备工作:      将上一节搭建的工程复制一份,命名为"5.key scanf".这一节主要讲如何使用SAM4N的GPIO输入功能,实现按键的输入. 二.程序编写:      ...

  3. mysql常用的一些命令,用于查看数据库、表、字段编码

    1.查看数据库支持的所有字符集         show character set;或show char set; 2.查看当前状态 里面包括当然的字符集设置         status或者\s ...

  4. cocos2d-x 2.1.4 使用create_project.py脚本创建项目+ant打包项目

    1.创建项目:执行create_project.py脚本,进入Doc界面输入下面的命令: cd D:\cocos2d-x-2.1.4\cocos2d-x-2.1.4\tools\project-cre ...

  5. PHP开发Apache服务器配置

    照此配置流程,绝对一路畅通,可保无虞. 昨天弄了个PHP小程序,想在本地跑一下测试,可是工作电脑没有安装环境,于是下载了一个wamp,一路畅通,Apache.Mysql.PHP就 全有了.启动wamp ...

  6. 20169210《Linux内核原理与分析》第十周作业

    第一部分:实验 进程的调度时机与进程的切换 操作系统原理中介绍了大量进程调度算法,这些算法从实现的角度看仅仅是从运行队列中选择一个新进程,选择的过程中运用了不同的策略而已. 对于理解操作系统的工作机制 ...

  7. [Redux] Using mapDispatchToProps() Shorthand Notation

    We will learn how to avoid the boilerplate code in mapDispatchToProps() for the common case where ac ...

  8. ViewPager中使用自定义的ListView实例

    这篇内容是上一篇的延续,因为在上一篇的测试ViewPager成功了,才能实现这一篇的和ListView合在一起使用 效果图如下: 不愿意说理论,直接上代码 1.清单文件 activity_main.x ...

  9. linux下配置php Apache mysql

    一 Apache部分 http://www.cnblogs.com/bluewelkin/p/3805107.html里面是纠正了原文的一些小错误,即可正常安装 1.su 命令2.安装apr-1.3. ...

  10. VS2015+TFS2015源代码管理

    使用Visual Studio连接TFS