android studio基础组件,学习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;

}

}

4c4fb8ee47e1ea13e068e2cab282d5fb.png

Notification

Notification是在你的应用常规界面之外的展示的信息。当app让系统发送一个信息的首先以图表 的形式显示在通知栏。要查看信息的详细需要进入通知抽屉(notificationdrawer)中查看。

a129c3ec4c689e6cfe03d70fc8f7515c.png

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

220e2202868b0adb3311b5dd0bb74519.png

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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值