【Android】Android基础

控件

TextView

  • layout_width 组件宽度
  • layout_height 组件高度
  • id 为TextView设置一个组件id
  • text 设置显示文本内容
  • textColor 设置字体颜色
  • textStyle 设置字体风格,三个可选值:normal(无效果)、bold(加粗)、italic(斜体)
  • textSize 字体大小,单位一般用sp
  • background 控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片
  • gravity 设置控件中内容的对其方向,TextView中是文字,ImageView中是图片等等

Button

  • Button继承自TextView
  • background 使用此属性前需要修改src -> main -> res -> values -> themes.xml中 style标签 的 parent属性,将其从Theme.MaterialComponents.DayNight.DarkActionBar修改为Theme.MaterialComponents.DayNight.DarkActionBar.Bridge
  • background可以使用StateListDrawable
  • android:backgroundTint 设置background颜色
  • android:foreground 设置前景色

EditText

  • android:hint 提示文字
  • android:textColorHint 提示文字颜色
  • android:inputType 输入类型
  • android:drawableXxxx 在输入框的xxxx位置添加图片
  • android:drawablePadding 设置图片与输入内容的间距
  • android:paddingXxxx 设置内容和边距的间距
  • android:background 输入框背景设置

ImageView

  • android:src 设置图片资源
  • android:maxHeight 设置最大高度
  • android:maxWidth 设置最大宽度
  • android:adjustViewBounds 调整view的界限
  • android:scaleType 设置图片缩放类型
    • fitStart 保持宽高比缩放图片,直到较长的边与图片的边长相等,缩放完成后将图片放在ImageView的左上角
    • fitCenter 默认值,同fitStart ,缩放后放于中间
    • fitEnd 同fitStart ,缩放后放于右下角
    • fitXY 对图像的横纵方向进行独立缩放,使得该图片完全适合ImageView,但是图片的宽高比可能会发生变化
    • center 保持原图的大小,显示在ImageView的中心。当原图的size大于ImageView的size,超过部分裁剪处理
    • centerCrop 保持宽高比缩放图片,直到完全覆盖ImageView,可能会出现图片的显示不完全
    • centerinside 保持宽高比缩放图片,直到ImageView能够完全地展示图片
    • matrix 不改变原图的大小,从ImageView的左上角开始绘制原图,原图超过ImageView的部分作裁剪处理

ProgressBar

  • android:max 进度条最大值
  • android:progress 当前进度
  • android:indeterminate 是否显示精确进度
  • style="@style/Widget.AppCompat.ProgressBar.Horizontal" 调整进度条样式为水平进度条

Toolbar

【注意】
themes.xml的style需要修改为Theme.MaterialComponents.DayNight.NoActionBar
【样式一】

<androidx.appcompat.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="#FFFF00"
    app:navigationIcon="@drawable/ic_baseline_account_circle_24"
    app:subtitle="子标题"
    app:title="标题"
    app:titleMarginStart="20dp"
    app:titleTextColor="#FF0000" />

【样式二】

    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#FFFF00"
        app:navigationIcon="@drawable/ic_baseline_account_circle_24"
        app:titleTextColor="#FF0000">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="标题"
            android:textSize="20sp" />
    </androidx.appcompat.widget.Toolbar>

AlertDialog

AlertDialog创建代码:AlertDialog.Builder builder = new AlertDialog.Builder(this); 
  • setIcon 添加icon
  • setTitle 添加标题
  • setMessage 添加消息主体
  • setPositiveButton 设置确认选项事件
  • setNeutralButton 设置中间选项事件
  • setNegativeButton 设置否定选项事件
  • create 创建AlertDialog
  • show 展示AlertDialog
  • setView 自定义布局

PopupWindow

View popupView = getLayoutInflater().inflate(R.layout.popup_view, null);
PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.showAsDropDown(view);
  • setContentView 设置PopupWindow显示的view
  • showAsDropDown 控制弹窗显示于某个控件的某个位置
  • setFocusable 设置是否获取焦点
  • setBackgroundDrawable 设置背景
  • dismiss 关闭弹窗
  • setAnimationStyle 设置加载动画
  • setTouchable 设置触摸功能
  • setOutsideTouchable 设置外部触摸功能

布局

LinearLayout

  • android:orientation 设置布局中组件排列方向,vertical(垂直排列),horizontal(水平排列)
  • android:gravity 控制组件的绝对排列方式
  • android:layout_gravity 控制组件在父容器中的相对排列方式
  • android:divider 设置分割线
  • android:showDividers 设置分线的展现位置
  • android:dividerPadding 设置分割线的padding
  • android:layout_weight 划分剩余空间的权重

RelativeLayout

【根据父容器定位】

  • android:layout_alignParentRight 右对齐

  • android:layout_alignParentLeft 左对齐

  • android:layout_alignParentTop 顶部对齐

  • android:layout_alignParentBottom 底部对齐

  • android:layout_centerHorizontal 水平居中

  • ndroid:layout_centerVertical 垂直居中

  • android:layout_centerInParent 中间位置
    【根据兄弟组件定位】

  • android:layout_toLeftOf 放置于参考组件的左侧

  • android:layout_toRightOf 放置于参考组件的右侧

  • android:layout_above 放置于参考组件上方

  • ndroid:layout_below 放置于参考组件下方

  • android:layout_alignTop 对齐参考组件的上边界

  • android:layout_alignBottom 对齐参考组件的下边界

  • android:layout_alignLeft 对齐参考组件的左边界

  • android:layout_alignRight 对齐参考组件的右边界

FrameLayout

  • android:foreground 设置前景色
  • android:foregroundGravity 设置前景色位置

TableLayout

【常见属性】

  • android:collapseColumns 设置被隐藏列
  • android:stretchColumns 设置被拉伸列
  • android:shrinkColumns 设置被收缩列
    【子控件属性】
  • android:layout_column 显示在第几列
  • android:layout_span 横向跨几列

GridLayout

【常见属性】

  • android:orientation 设置组件水平显示还是垂直显示
  • android:columnCount 设置每行显示多少列
  • android:rowCount 设置每列显示多少行
    【子控件属性】
  • android:layout_column 显示在第几列
  • android:layout_columnSpan 横向跨第几列
  • android:layout_columnWeight 横向剩余空间分配方式
  • android:layout_gravity 在网格中的显示位置
  • android:layout_row 显示在第几行
  • android:layout_rowSpan 纵向跨几行
  • android:layout_rowWeight 纵向剩余空间分配方式

ConstraintLayout

约束布局

其他

StateListDrawable

  • drawable 引用的Drawable位图
  • state_focused 是否获取焦点
  • state_pressed 控件是否被按下
  • state_enabled 控件是否可用
  • state_selected 控件是否被选择,针对有滚轮的情况
  • state_checked 控件是否已经被勾选
  • state_checkable 控件可否被勾选
  • state_window_focused 是否获得窗口焦点
  • state_active 控件是否处于活动状态
  • state_single 控件包含多个子控件时,是否只展现一个子控件
  • state_first 控件包含多个子控件时,第一个子控件是否处于显示状态
  • state_middle 控件包含多个子控件时,中间子控件是否处于显示状态
  • state_last 控件包含多个子控件时,最后一个子控件是否处于显示状态

消息通知

Notification

【基础概念】
Notification 通知是应用向用户显示的消息提示,当发送通知时,通知将先以图标的形式显示在通知区域中。用户可以打开下拉通知栏查看通知的详细信息。通知区域和下拉通知栏均是由系统控制的区域,用户可以随时查看。
【代码实现】

Notification notification = new NotificationCompat
                .Builder(this, channelId)
                .build();

【常见方法】

  • setContentTitle 设置标题
  • setContentText 设置文本内容
  • setSmallIcon 设置左侧小图标
  • setLargeIcon 设置右侧大图标
  • setColor 设置小图标颜色
  • setContentIntent 设置点击通知后的跳转意向
  • setAutoCancel 设置点击通知后自动清除通知
  • setWhen 设置通知被创建的时间

NotificationManager

【基础概念】
NotificationManager是一个通知管理器,被系统以单例的方式创建出来。
【代码实现】

NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATON_SERVICE);

【常见方法】

  • createNotificationChannel 将创建好的 NotificationChannel 放入NotificationManager中
  • notify 用来发送通知的方法

NotificationChannel

【基础概念】
在Android8.0后作为通知渠道发布通知
【代码实现】

NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);

【常见方法】

代码片段

片段1

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel notificationChannel = new NotificationChannel(channelId, "测试通知", NotificationManager.IMPORTANCE_HIGH);
    notificationManager.createNotificationChannel(notificationChannel);
}

片段2

Intent intent = new Intent(this, TargetActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

注意

  • NotificationChannel的id与Notification的id必须相同

ListView

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值