android入门到熟练(三)----UI界面
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界面的更多相关文章
- Android开发1:基本UI界面设计——布局和组件
前言 啦啦啦~本学期要开始学习Android开发啦~ 博主在开始学习前是完完全全的小白,只有在平时完成老师要求的实验的过程中一步一步学习~从此篇博文起,博主将开始发布Android开发有关的博文,希望 ...
- Android研究之动态创建UI界面具体解释
Android的基本UI界面一般都是在xml文件里定义好,然后通过activity的setContentView来显示在界面上.这是Android UI的最简单的构建方式.事实上,为了实现更加复 ...
- Android开发精彩博文收藏——UI界面类
本文收集整理Android开发中关于UI界面的相关精华博文,共大家参考!本文不定期更新! 1. Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各 ...
- Android 子线程无法刷新UI界面
问题:在Android开发中,子线程无法直接更改UI界面视图的刷新 这个时候 Handler 起到了至关重要的作用. 简单来说 , Handler就是用来传递消息的. Handler可以当成子线程与主 ...
- Android Studio 图形化设计 UI 界面
我们开发 Android 程序必定是从 UI 开始的 ,使用最新版的 Android Studio 可以在图形化界面下设计软件 UI, Android Studio 默认的布局是 Constraint ...
- android入门到熟练(一)
1.andro系统架构:Linux内核层(提供驱动),系统运行库层和android运行时库(提供C/C++库的主要特性,如SQLite,OpenGL,Webkit等和Dalvik虚拟机),应用框架层, ...
- Android入门(十):界面的布局方式及其实际应用
关于Android界面布局,网上已经有了很多非常不错的学习资料,在这里我也不班门弄斧了,推荐两篇我认为写的不错的教程,然后再重点讲一下几种布局方式的实际应用. 教程链接:①http://www.cnb ...
- android入门到熟练(二)----活动
1.活动创建对于每一个后端java类(继承至Activity或者ActionBarActivity)代码都有一个方法需要被重写[onCreate], 在此方法中可以加载界面资源文件或者绑定元素事件. ...
- Android Studio 屏幕方向以及UI界面状态的保存
package com.example.orientation; import android.os.Bundle; import android.util.Log; import android.v ...
随机推荐
- Ubuntu 下安装Kibana和logstash
原文地址:http://www.cnblogs.com/saintaxl/p/3946667.html 简单来讲他具体的工作流程就是 logstash agent 监控并过滤日志,将过滤后的日志内容发 ...
- 从源码编译rpi的内核
Kernel Building https://www.raspberrypi.org/documentation/linux/kernel/building.md There are two mai ...
- Web开发,如何从小工到专家
最近在研读关于“整体性学习”的一些东西,收获颇丰. 整体性学习强调的东西有三样:结构.模型.与高速通道.特别是关于结构的篇章: 理解是什么?理解就是结构高度发达完善的结果. 是不是有些学科你可以轻松“ ...
- 宁波Uber优步司机奖励政策(2月1日~2月7日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- bash的多行注释
:<<EOF 注释的代码... EOF 单行是#
- 1115 HTML CSS
1. HTML 全称HyperText Markup Language (超文本标记语言). 2. 网页=HTML文件 + Web服务器 + CSS文本. 3. Web服务器:处理浏览器请求,寻找资源 ...
- FZU2138-久违的月赛之一
Problem Description 好久没举月赛了,这次lqw给大家出了5道题,因为hsy学长宣传的很到位,吸引了n个DDMM们来做,另一位kk学长说,全做对的要给金奖,做对4题要给银奖,做对3题 ...
- Java工作队列和线程池
背景 最近的需要做一个与设备通信的web项目.当然,我们需要写好与设备之间的通信协议(socket).大致的时序逻辑时:当用户用浏览器点击页面某一控件后,它就向后台发送一个post请求,后台解析 ...
- 使用泛型简单封装NGUI的ScrollView实现滑动列表
懒,是老毛病了,周末跑了半马,跑完也是一通累,好久没锻炼了..也是懒的,有时都懒的写博客..最近看到项目中各种滑动列表框,本着要懒出水平来的原则,决定花点时间简单处理下(暂时未做列表太多时的优化):1 ...
- oppo X907刷机包 COLOROS 1.0 正式版公布 安卓4.2.2
ROM介绍 本版本号将是X907史上最好的一版本号 全新COLOROS的UI 更新全局手势板操作 优化高速启动应用 安全保障中心也是一直採用COLOROS组成的 COLOROS 1.0给用户带来在线音 ...