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. 用 localhost 访问正常,替换成 IP ,部分 CSS 或 JS 就失效了

    这应该是浏览器的兼容性问题. 经测试,只要不是360浏览器的兼容模式,将 localhost 替换成 IP 无影响. 来自为知笔记(Wiz)

  2. 驱动lx4f120h,头文件配置,没有完全吃透,望指点

    来了块开发板,没接触过,希望能驱动起来,就首先试一下驱动LED,没想到刚开始建好工程问题就来了 使用GPIO驱动,首先想到的是关于GPIO的头文件gpio.h,事实上这个还不够,还需要设置一下系统的配 ...

  3. 三种排序算法python源码——冒泡排序、插入排序、选择排序

    最近在学习python,用python实现几个简单的排序算法,一方面巩固一下数据结构的知识,另一方面加深一下python的简单语法. 冒泡排序算法的思路是对任意两个相邻的数据进行比较,每次将最小和最大 ...

  4. C#使用Sockets操作FTP【转载】

    using System; using System.Collections; using System.IO; using System.Net; using System.Net.Sockets; ...

  5. APUE 读书笔记 -----孤儿进程与僵尸进程[总结] +数据结构+C

    http://www.cnblogs.com/Anker/p/3271773.html

  6. cocos2d-x Lua与OC互相调用

    1. Lua 调用OC 先看例子: hello.lua: -- 点击回调函数 local function notifymenuCallbackTest() local luaoc = require ...

  7. Android开发之使用意图调用内置应用程序

    意图可以调用活动,也常被用来调用内置应用程序,如加载web页面,拨号页面,内置地图应用等等.下面就用例子来说明该用法. 效果图如下: 实现代码如下: 上图中的启动MyBrowser是用意图来调用MyW ...

  8. Android(java)学习笔记193:利用谷歌API对数据库增删改查(推荐使用)

    接下来我们通过项目案例来介绍:这个利用谷歌API对数据库增删改查 1.首先项目图: 2.这里的布局文件activity_main.xml: <LinearLayout xmlns:android ...

  9. JDBC操作数据库 封装好的工具类

    mysql sqlserver oracle 数据库的驱动jar包http://download.csdn.net/download/csdn576038874/8833683package cn.h ...

  10. LCA问题

    基本概念 LCA:树上的最近公共祖先,对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. RMQ:区间最小值查询问题.对于长度为n的 ...