Android-控件
TextView
- 文本内容 - text
Button
点击切换背景图片
drawable/bg.xml
1
2
3
4
5
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/actionsheet_bottom_selector" android:state_pressed="true"></item>
<item android:drawable="@drawable/anim_connecting"></item>
</selector>1
android:background="@drawable/bg"
点击切换背景颜色
color/bg_color.xml
1
2
3
4
5
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/bg_grey" android:state_pressed="true"></item>
<item android:color="@color/black" ></item>
</selector>1
android:backgroundTint="@color/bg_color"
事件顺序:touch_down > longclick > touch_up > click
click 事件最后响应
touch 和 longclick 事件可以终止事件传播,
return true
即可touch 事件
event.getAction()
0-down 1-up1
2
3//x、y 为 touch 在屏幕上的位置
float x = event.getX();
float y = event.getY();Drag 拖拽事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public boolean onDrag(View v, DragEvent event) {
final int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED: // 拖拽开始
return event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
case DragEvent.ACTION_DRAG_ENTERED: // 被拖拽View进入目标区域
return true;
case DragEvent.ACTION_DRAG_LOCATION: // 被拖拽View在目标区域移动
return true;
case DragEvent.ACTION_DRAG_EXITED: // 被拖拽View离开目标区域
return true;
case DragEvent.ACTION_DROP: // 放开被拖拽View
String content = event.getClipData().getItemAt(0).getText().toString(); //接收数据
Toast.makeText(this,content,Toast.LENGTH_SHORT).show();
return true;
case DragEvent.ACTION_DRAG_ENDED: // 拖拽完成
return true;
}
return false;
}
EditText
hint 输入提示
textColorHint 提示颜色
inputType 类型
backgroundTint: hint 下划线背景
1
android:backgroundTint="@color/white"
drawableXXX 指定方位添加图片
drawablePadding 图片内边距
ImageView
- src 设置图片源
- scaleType 缩放类型
- maxHeight 最大高度
- maxWidth 最大宽度
- adjustViewBounds 调整 view 的界限
ProgressBar
- max 进度条最大值
- progress 当前进度值
- Indeterminate 设置成 true,不显示精确进度
- style=”?android:attr/progressBarStyleHorizontal” 水平进度条
Notification
发送通知、取消通知
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30//获取 NotificationManager
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
final String channelId = "scm";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//android 8及以上版本
NotificationChannel notificationChannel = new NotificationChannel(channelId, "通知信息", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);
}
//创建 PendingIntent
Intent intent = new Intent(this, MainActivity2.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
//构建通知对象
Notification notification = new NotificationCompat.Builder(this, channelId)
.setContentTitle("通知")
.setContentText("通知内容")
.setSmallIcon(R.drawable.app_icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_stat_name)) //设置大图标
.setColor(Color.BLUE) //设置小图标颜色。因为 app_icon 是无颜色的
//.setColor(Color.parseColor("#ff0000")) 设置小图标红色
.setContentIntent(pendingIntent) //设置点击后跳转意图
.build();
//发送通知
notificationManager.notify(1, notification);
//取消通知
notificationManager.cancel(1); //参数 ID 必须与发送一致setContentTitle(String s) 设置标题
setContentText(String s) 设置文本内容
setSmallIcon(int icon) 设置小图标
setLargeIcon(Bitmap icon) 设置大图标
setColor(int argb) 设置小图标颜色
setContentIntent(PendingIntent intent) 点击通知后跳转意图
setAutoCancel(boolean b) 点击通知后是否自动消除通知
setWhen(long when) 设置通知被创建的时间
注意:Android 5.0 开始,对于通知栏图标的设计进行了修改。现在只能使用 Alpha
图层绘制,而不应该包括 RGB图层。即图片不能带颜色
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 王文哲的博客!