UI Components总结

一:UI Components介绍

1.是什么:

用于呈现View的一些组件,并不是View但是可以更好的呈现View。

2.主要包括:

(1)Menu菜单:

①Option Menu(选项菜单) ②Context Menu(上下文菜单) ③popup Menu(弹出式菜单)

(2)ActionBar(动作栏)

(3)Dialog(对话框)

①AlertDialog(提示对话框) ②DatePickerDialog(日期对话框)  ③ProgressDialog(进度对话框)

(4)Notifiction(通知)

(5)Toast

二:Menu菜单

1.Option Menu 选项菜单

实现步骤:

①重写onCreatOptionsMenu()方法;

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }

②设置监听:重写onOptionsItemSelected()方法;

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

注意:需要判断点击位置即通过判断item.getItemId和点击的菜单的id比较从而触发相应事件;

2.Context Menu 上下文菜单

实现步骤:

①获得ViewGroup对象,并关联适配器,存放数据源;

②给容器View注册上下文菜单:

registerForContextMenu(如ListView lv);//注册上下文菜单;(基于ListView的长按事件;长按就会弹出上下文菜单)

③创建菜单:

//当长按注册了上下文菜单的view时,此方法会执行;
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.ctx, menu);
    }

④处理上下文菜单的点击事件:
  @Override
    public boolean onContextItemSelected(MenuItem item) {
        int id=item.getItemId();
        if(id==R.id.action_settings1){
            AdapterContextMenuInfo info=(AdapterContextMenuInfo) item.getMenuInfo();
            String text=list.get(info.position);//点击该菜单弹出该item中的信息;
            Toast.makeText(this, text, 0).show();
        }else if(id==R.id.action_settings2){
            Toast.makeText(this, "delete", 0).show();
            //获得要删除的item相关信息;(列入位置)
            //从数据集中删除数据,更新listView
            AdapterContextMenuInfo info=(AdapterContextMenuInfo) item.getMenuInfo();
            list.remove(info.position);
            ArrayAdapter adapter=(ArrayAdapter<?>)lv.getAdapter();//获得ListView关联的适配器;
            adapter.notifyDataSetChanged();//更新ListView
        }
        return super.onContextItemSelected(item);
    }

3.弹出式菜单:PopupMenu

当点击比如Button按钮时弹出:

private Button bt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt=(Button) findViewById(R.id.bt);
        bt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
                //构建PopupMenu对象;
                PopupMenu pm=new PopupMenu(MainActivity.this, v);
                //构建菜单项:
                pm.getMenuInflater().inflate(R.menu.main, pm.getMenu());
                //设置监听:
                pm.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        ((Button)v).setText(item.getTitle());
                        return false;
                    }
                });
                //显示菜单:
                pm.show();
            }
        });
    }

三.ActionBar 动作栏

1.介绍:

android 3.0推出的一个新的对象;此对象最主要的目的是在标题栏更好的为用户呈现一些view组件,并实现与用户的交互,例如可以呈现Menu对象;

2.使用:

①获得ActionBar对象,前提是该Activity必须为包含ActionBar的主题;

ActionBar bar=getActionBar();

②操作ActionBar:

private void setActionBar() {
        //获得ActionBar对象:
        bar=getActionBar();
        //设置home可用:
        bar.setDisplayShowHomeEnabled(true);
        //设置home图标:
        bar.setIcon(R.drawable.jiantouright);
        bar.setDisplayUseLogoEnabled(true);//设置显示logo;
        bar.setHomeButtonEnabled(true);//设置logo可点击;
        bar.setNavigationMode(ActionBar.DISPLAY_SHOW_CUSTOM);//设置导航模式为自定义view;
        bar.setDisplayShowCustomEnabled(true);//设置使用自定义view;
        tv=new TextView(this);
        tv.setTextSize(20);
        tv.setText(R.string.secondactivity_menu_title);
        bar.setCustomView(tv,new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT,
                Gravity.CENTER
                ));
    }

③自定义主题样式:

<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">
           @style/MyActionBarStyle
        </item>
    </style>
    
    <style name="MyActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar.Solid">
        <item name="android:background">@color/white</item>
        <item name="android:displayOptions"></item>
    </style>

四.Drawlayout 侧滑layout

1.布局配置:

<android.support.v4.widget.DrawerLayout >

<!--设置非侧滑内容的布局-->

<FramLayout></FramLayout>

<!--设置侧滑内容的布局-->

<ListView></ListView>

</android.support.v4.widget.DrawerLayout >

注意:DrawerLayout在布局中必须是以根元素存在!在该布局中很多属性不能通过Alt+?自动补全,可以在先在另一个布局中写完后复制过来;

<ListView>中要写:android:layout_gravity="left/right......"从哪个方向滑出;

2.java代码:

(1)关联DrawerLayout源码方式:libs目录下新建一个名为:android-support-v4.jar.properties的文件

文件中写:android.support.v4.src的目录;

如:src=C:\\software\\android-sdk-windows\\extras\\android\\support\\v4\\src

关闭所有eclipse窗口,关闭工程,然后打开;

(2)实现步骤:

①侧滑菜单中添加数据;

②设置侧滑:

private void setDrawerLayout() {
        //获得DrawerLayout对象:
        dl=(DrawerLayout) findViewById(R.id.dl_secondLayout);
        //设置监听:
        dl.setDrawerListener(new DrawerListener() {
            @Override
            public void onDrawerStateChanged(int arg0) {
            }
            @Override
            public void onDrawerSlide(View arg0, float arg1) {
            }
            @Override
            public void onDrawerOpened(View arg0) {
                bar.setLogo(R.drawable.jiantouleft);
            }
            @Override
            public void onDrawerClosed(View arg0) {
                bar.setLogo(R.drawable.jiantouright);
            }
        });
    }

五.对话框Dialog

1.AlertDialog:提示对话框

弹出提示信息,如在退出时,删除某项内容时弹出....

 AlertDialog ad=new AlertDialog.Builder(MainActivity.this).setTitle("退出")
             //.setMultiChoiceItems(items, checkedItems, listener)设置多选菜单;
             .setMessage("确定退出吗?").
             setPositiveButton("确定", new OnClickListener() {
                    
                 @Override
                 public void onClick(DialogInterface dialog, int which) {
                     MainActivity.super.onBackPressed();//点击回退按钮时执行;
                 }
              }).
               setNegativeButton("取消", null).show();
               return false;

2.DatePickerDialog 日期对话框

①构建DatePickerDialog对象;

②设置相关监听事件;

③将日期对话框内容回调;

④设置显示日期样式;

final Calendar cal=Calendar.getInstance();
                int year=cal.get(Calendar.YEAR);
                final int month=cal.get(Calendar.MONTH);
                final int day=cal.get(Calendar.DAY_OF_MONTH);
                new DatePickerDialog(MainActivity.this,
                        new OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                //((Button)v).setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
                                setDateText(cal,year,monthOfYear,dayOfMonth,v);
                            }
                            
                            //参照DatePickerDialog对话框中的updateTitle(int year, int month, int day)方法;
                            //设置Button显示的日期样式;
                            private void setDateText(Calendar cal, int year, int monthOfYear, int dayOfMonth, View v) {
                                cal.set(Calendar.YEAR, year);
                                cal.set(Calendar.MONTH, monthOfYear);
                                cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                                String title=DateUtils.formatDateTime(MainActivity.this,
                                        cal.getTimeInMillis(),
                                        DateUtils.FORMAT_SHOW_DATE|DateUtils.FORMAT_SHOW_WEEKDAY|
                                        DateUtils.FORMAT_SHOW_YEAR|DateUtils.FORMAT_ABBREV_ALL);
                                ((Button)v).setText(title);
                            }
                        },
                        year,
                        month,//在java里面以0开头的数字以八进制形式表示;
                        day)
                .show();
                            
            }

3.对话框形式的activity:

实现方式:主要是通过修改清单配置文件中activity的对话框形式的主题来实现;

如: android:theme="@android:style/Theme.Holo.Light.Dialog"    

六.Notifiction 通知

1.介绍:

(1)Notification是什么:

1)手机状态栏显示的一个对象;(最上面的显示电量,信号等的内容)

2)用于呈现View的一个对象;

(2)应用场合:一般应用于安卓的服务器端(service);可以在service端创建,并发送,然后显示在状态栏;

 例如:软件更新通知,短信的通知,未接电话的通知,播放音乐的通知,新闻头条等。。。。。。

(3)如何使用:

1)构建该对象;(借助builder对象)

2)发送该对象;(借助NotificationManager对象)(NofitificationManager)

3)操作该对象;(事件处理通过PendingIntent)

      ①:PendingIntent

4)定义Notification布局:

 a)RemoteViews

 b)setXXXX;

Notification nft=new Notification.Builder(this).setSmallIcon(R.drawable.ic_launcher)//必须要
        .setContentInfo("Title")
        .setContentText("Content")
        //设置延迟意图点击通知时执行操作,不点击则不执行;
        .setContentIntent(PendingIntent.
                getActivity(this, 250,
                        new Intent(this,OtherActivity.class) ,
                            PendingIntent.FLAG_UPDATE_CURRENT))
        .setTicker("地震了!")//引爆消息
        .build();//创建通知;
        //nft.flags=Notification.FLAG_NO_CLEAR;//通知不允许你清除;
        
        nft.flags=Notification.FLAG_AUTO_CANCEL;//一般只对启动activity有效;点击通知启动activity然后自动销毁;
        
        //发送通知:
        NotificationManager nMagager=//获得系统服务;
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nMagager.notify(0, nft);//给通知指定一个id;
        //nMagager.cancel(0);//关闭某个通知;
    }

七.Toast 通知

1.介绍:

(1)安卓中的消息对象;用于呈现具体View信息;

(2)需要弹出消息的时候;给出正确或者错误的提示;

2.系统中的Toast

//系统吐丝;
    public void onClick(View v){
        Toast.makeText(this, "吐丝", 0).show();
        Toast.makeText(this, 0x7f050002, 0).show();
        //Toast.makeText(this, 100, 0).show();编译时不会出错,运行时找不到就会出错!!!
        Toast.makeText(this, R.string.app_name, 0).show();//int类型不能随意写,
        //必须是string.xml下的某个资源的id;如果没有找到该id运行时报错;
    }

3.自定义Toast:

    //自定义吐丝;
    public void onClick2(View v){
        Toast t=new Toast(this);
        t.setGravity(Gravity.CENTER, 0, -100);//对手机屏幕来说左上角为(0,0)坐标原点位置;
        TextView tv=new TextView(this);
        //ImageView iv=new ImageView(this);
        tv.setGravity(Gravity.CENTER);//内容居中;
        Drawable top=getResources().getDrawable(R.drawable.ic_launcher);//获得Drawable对象图片;
        top.setBounds(0,0,top.getMinimumWidth(),top.getMinimumHeight());//必须给定drawable一个边界;
        tv.setCompoundDrawables(null, top, null, null);//文本上下左右都可以放图片;
        tv.setText(R.string.hello_world);//需要新建一个TextView,然后在Toast对象中加入这个TextView;
        //iv.setImageResource(R.drawable.ic_launcher);
        //t.setView(iv);//可以添加图片或者TextView,吐图片或者文本;
        t.setView(tv);//setView(tv)会覆盖ImageView;
        t.setDuration(1);
        t.show();

如有疑问欢迎提出宝贵意见!谢谢!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值