学习Android studio基础高级组件

学习Android studio的基础高级组件

经过几个星期的学习,基础部分的Android studio的学习也已经进入了尾声。

PopupWindow

Android的对话框有两种,分别为PopupWindow和AlertDialog。PopupWindow的位置按照有无偏移分为两种,参照物不同也可分为相对某控件和相对父孔控件两种。

showAsDropDown(View anchor) 相对某个控件的位置(正左下方)
showAsDropDown(View anchor,int xoff,int x,int y) 相对某控件的位置
showAsDropDown(View parent,int gravity,int x,int y) 相对于父控件的位置
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //按钮事件方法
    public void showWindow(View v){

        View view=getLayoutInflater().inflate(R.layout.popup_window_layout,null);
        //创建PopupWindow(窗体的视图,宽,高)
        PopupWindow popupWindow=new PopupWindow(view,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);

        popupWindow.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default));
        popupWindow.setAnimationStyle(android.R.style.Animation_Translucent);
        popupWindow.getBackground().setAlpha(100);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        popupWindow.showAtLocation(v,Gravity.BOTTOM,0,0);

        //获取屏幕尺寸
        DisplayMetrics dm =new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width=dm.widthPixels;
        int height=dm.heightPixels;
    }
}

学习Android studio基础高级组件_第1张图片

Notification

Notification是在你的应用常规界面之外的展示的信息。当app让系统发送一个信息的首先以图表 的形式显示在通知栏。要查看信息的详细需要进入通知抽屉(notificationdrawer)中查看。
学习Android studio基础高级组件_第2张图片

 public void sendNotification1(View v) {
        Notification.Builder builder = new Notification.Builder(this);
        NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("你有一条新信息");
        builder.setContentText("新年快乐!");
        builder.setOngoing(true);
        builder.setAutoCancel(true);
        builder.setDefaults(Notification.DEFAULT_ALL);
        builder.setNumber(10);
        builder.setTicker("新信息");
        Notification n = builder.build();
        @SuppressLint("ServiceCast") NotificationManager nm = (NotificationManager) getSystemService(Context.NETWORK_STATS_SERVICE);
        nm.notify(NID_1, n);

文字类的显示。

public void sendNotification3(View v) {
        final Notification.Builder builder = new Notification.Builder(this);
        NotificationCompat.Builder builder3 = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("更新中。。。");
        builder.setContentText("正在更新至最新版本");
        builder.setProgress(100, 5, false);
        @SuppressLint("ServiceCast") final NotificationManager nm = (NotificationManager) getSystemService(Context.NETWORK_STATS_SERVICE);
        nm.notify(NID_3, builder.build());
        new Thread(new Runnable() {
            @Override
            public void run() {
               // int progress = 0;
                for(int progress =0;progress<=100;progress+=5){
                    builder.setProgress(100,progress,false);
                    nm.notify(NID_3,builder.build());
                    try{
                        Thread.sleep(500);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
                builder.setProgress(0,0,false);
                builder.setContentText("更新完成。");
            }
        }).start();
    }

进度条类的通知显示

 Notification.Builder builder = new Notification.Builder(this);
        NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("你有一条新信息");
        builder.setContentText("消息");
        //设置大视图样式
        NotificationCompat.InboxStyle style=new NotificationCompat.InboxStyle();
        style.setBigContentTitle("吟诗作对");
        style.addLine("");
        style.addLine("");
        style.addLine("");
        builder.setStyle(style);

大视图的类型

GridView

学习Android studio基础高级组件_第3张图片
GridView组件用来以网格方式排列视图,与矩阵类似。

numColumns=“auto_fit” 列数设置为自动
columnWidth=“90dp” 每一列的宽度,就是item的宽度
stretMode=“columnWidth” 缩放与列宽大小同步
verticalSpacing 两行之间的边距
horizontalSpacing 两列之间的边距
 static class MyAdapter extends BaseAdapter{
        private int[] images={R.drawable.qq_1,
                R.drawable.qq_2,
                R.drawable.qq_3,
                R.drawable.qq_4,
                R.drawable.qq_5,
                R.drawable.qq_6,
                R.drawable.qq_7,
                R.drawable.qq_8,R.drawable.qq_9};

        private Context context;
        public MyAdapter(Context context){
            this.context=context;
        }
        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public Object getItem(int position) {
            return images[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ImageView iv=new ImageView(context);
            iv.setImageResource(images[position]);
            return iv;

你可能感兴趣的:(周记)