一、使用ViewPager开发新特性引导界面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager
android:id="@+id/my_view_pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:overScrollMode="never"> </android.support.v4.view.ViewPager>
</RelativeLayout>

viewpager_index.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <ImageView
android:id="@+id/item_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

item_view_pager.xml

package com.ouc.wkp.ui1;

import android.app.Activity;
import android.graphics.Color;
import android.media.Image;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView; import java.util.ArrayList;
import java.util.List; /**
* Created by wkp on 2016/8/24.
*/
public class ViewPagerDemo extends Activity {//开发新特性引导界面 ViewPager viewPager; List<View> viewList = new ArrayList<>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_index); viewPager = (ViewPager) findViewById(R.id.my_view_pager); {
View view = LayoutInflater.from(this).inflate(R.layout.item_view_pager, null);
ImageView imageView = (ImageView) view.findViewById(R.id.item_image_view);
imageView.setBackgroundColor(Color.RED);
viewList.add(view);
}
{
View view = LayoutInflater.from(this).inflate(R.layout.item_view_pager, null);
ImageView imageView = (ImageView) view.findViewById(R.id.item_image_view);
imageView.setBackgroundColor(Color.GREEN);
viewList.add(view);
}
{
View view = LayoutInflater.from(this).inflate(R.layout.item_view_pager, null);
ImageView imageView = (ImageView) view.findViewById(R.id.item_image_view);
imageView.setBackgroundColor(Color.BLUE);
viewList.add(view);
} viewPager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return viewList.size();
} @Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
} @Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position));
return viewList.get(position);
} @Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewList.get(position));
}
});
}
}

ViewPagerDemo.java

运行效果就是红蓝绿三页左右滑动不方便演示。具体开发时请使用好看的图片。

二、使用Menu编写菜单,注意创建模拟器的时候在skin中设置skin with dynamic hardware controls。

首先我们在res文件夹下创建menu文件夹,再在menu文件下创建Menu resource file

创建xml编写菜单项

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/main_menu_1"
android:icon="@drawable/menu1"
android:title="菜单一"/> <item android:id="@+id/main_menu_2"
android:icon="@drawable/menu2"
android:title="菜单二"/> <item android:id="@+id/main_menu_3"
android:icon="@drawable/menu3"
android:title="菜单三"/> </menu>

main_menu.xml

一个空壳布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> </LinearLayout>

menu_index

主程序

package com.ouc.wkp.ui1;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast; import java.lang.reflect.Method; /**
* Created by wkp on 2016/8/24.
*/
public class MenuDemo extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_index);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// menu.add("菜单1");
// menu.add("菜单2");
// menu.add("菜单3");
// menu.add("菜单4");
setIconEnable(menu,true);
new MenuInflater(this).inflate(R.menu.main_menu,menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.main_menu_1:
//处理第一个菜单的点击事件
Toast.makeText(this,"用户点击了"+item.getTitle(),Toast.LENGTH_SHORT).show();
break;
case R.id.main_menu_2:
Toast.makeText(this,"用户点击了"+item.getTitle(),Toast.LENGTH_SHORT).show();
break;
case R.id.main_menu_3:
Toast.makeText(this,"用户点击了"+item.getTitle(),Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
} //enable为true时,菜单添加图标有效,enable为false时无效,4.0系统默认无效
private void setIconEnable(Menu menu,boolean enable){
try{
Class<?> clazz=Class.forName("com.android.internal.view.menu.MenuBuilder");
Method m=clazz.getDeclaredMethod("setOptionalIconsVisible",boolean.class);
m.setAccessible(true); //MenuBuilder实现Menu接口,创建菜单时,传进来的menu其实就是MenuBuilder对象(java多态)
m.invoke(menu,enable);
}catch(Exception e){
e.printStackTrace();
}
}
}

MenuDemo.java

注意我们写一个方法

然后通过setIconEnable(menu,true)来为菜单添加图标

下面的代码是弹出提示

运行效果

三、使用PopupWindow写弹出窗

总体布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:id="@+id/btn_pop_window"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="弹出popWindow"/>
</RelativeLayout>

pop_window_index.xml

弹出窗布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bbbbbb"> <TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/tv_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <Button
android:id="@+id/btn_cancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"/> <Button
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"/>
</LinearLayout>
</LinearLayout>

pop_window.xml

主程序

package com.ouc.wkp.ui1;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast; /**
* Created by wkp on 2016/8/25.
*/
public class PopWindowDemo extends Activity{ PopupWindow popupWindow; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop_window_index); findViewById(R.id.btn_pop_window).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//弹出
// popupWindow.showAsDropDown(view);
//进行屏幕居中显示
popupWindow.showAtLocation(PopWindowDemo.this.getWindow().getDecorView(), Gravity.CENTER,0,0);
}
}); View view= LayoutInflater.from(this).inflate(R.layout.pop_window,null);
((TextView)view.findViewById(R.id.tv_title)).setText("标题");
((TextView)view.findViewById(R.id.tv_msg)).setText("这里是popwindow显示的消息内容"); //点击关闭
view.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"您点击了确定按钮",Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
}); view.findViewById(R.id.btn_cancle).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"您点击了取消按钮",Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
}); popupWindow=new PopupWindow(view, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT);
//点击外部消失
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setFocusable(true);
}
}

PopWindowDemo.java

运行效果

android入门——UI(6)——ViewPager+Menu+PopupWindow的更多相关文章

  1. Android开发UI之ViewPager及PagerAdapter

    ViewPager,官网链接--http://developer.android.com/reference/android/support/v4/view/ViewPager.html ViewPa ...

  2. Android入门——UI(8)——Fragment(2)

    先演示一下如何在一个activity中放置两个Fragment,先定义两个Fragment <?xml version="1.0" encoding="utf-8& ...

  3. Android入门——UI(9)

    SwipRefreshLayout下拉刷新控件 <?xml version="1.0" encoding="utf-8"?> <android ...

  4. Android入门——UI(7)——Fragment

    先上fragment静态加载的代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  5. android入门——UI(5)

    最近时间实在匆忙,博客的代码基本没有解释. 介绍ExpandableListView <?xml version="1.0" encoding="utf-8&quo ...

  6. android入门——UI(4)

    GridView控件实现菜单 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...

  7. android入门——UI(3)

    Spinner控件   ListView控件 一.Spinner控件 点击Spinner会弹出一个包含所有可选值的dropdown菜单,从该菜单中可以为Spinner选择一个新值. 有两种指定数据源的 ...

  8. Android入门——UI(2)

    介绍SeekBar拖动条控件.ProgressBar进度条控件.DatePicker日历控件.TimePicker时间控件 <?xml version="1.0" encod ...

  9. android入门——UI(1)

    一.使用TextView ImageView Button EditView做出登录页面 <?xml version="1.0" encoding="utf-8&q ...

随机推荐

  1. javascritp第十课:面向对象

    js中的函数就是对象,对象就是函数,当js中需要使用面向对象,使用js闭包模拟面向对象,当函数作为对象使用时,每个单词首字母都大写 var obj=new object();  //js中默认就是ob ...

  2. iOS 唯一设备号

    https://github.com/fabiocaccamo/FCUUID 目前比较好的解决方案.

  3. Android读写JSON格式的数据之JsonWriter和JsonReader

    近期的好几个月都没有搞Android编程了,逐渐的都忘却了一些东西.近期打算找一份Android的工作,要继续拾起曾经的东西.公司月初搬家之后就一直没有网络,直到今日公司才有网络接入,各部门才開始办公 ...

  4. iOS音频播放(一):概述

    (本文转自码农人生) 前言 从事音乐相关的app开发也已经有一段时日了,在这过程中app的播放器几经修改,我也因此对于iOS下的音频播放实现有了一定的研究.写这个 系列的博客目的一方面希望能够抛砖引玉 ...

  5. 《think in python》学习-5

    think in python -5 think in python -5 条件和递归 求模操作符% 用于整数,可以计算出第一个操作数除以第二个操作数的余数 7%3 #结果是2 求模操作符%有很多用途 ...

  6. node.js(七) 子进程 child_process模块

    众所周知node.js是基于单线程模型架构,这样的设计可以带来高效的CPU利用率,但是无法却利用多个核心的CPU,为了解决这个问题,node.js提供了child_process模块,通过多进程来实现 ...

  7. 关于Resharper的使用经验

    发现Resharper这东西真的有点累赘,重构也是,一开始用会有很多莫名其妙的提示.现在的项目用了Resharper,js是很方便,有定位功能,但连TypeScript的js都有了.

  8. 监控concurrent 正在执行的sql

    SELECT a.sid, a.serial#, b.sql_text   FROM v$session a, v$sqltext b WHERE a.sql_address = b.address  ...

  9. read和onload jquery.val

    $(document).load(); 当web页面以及其附带的资源文件,如CSS,Scripts,图片等,加载完毕后执行此方法.常用于检测页面(及其附带资源)是否加载完毕. $(document). ...

  10. [一道搜狗输入法的面试题]C++转换构造函数和类型转换函数

    今天面试遇到一道有关C++转换构造函数的题目,之前经常见到默认构造函数.拷贝构造函数.析构函数,但是从没听说过转换构造函数,隐式转换函数也是一样,C++的确是够博大精深的,学习之路很长啊! 其实我们已 ...