一、通用属性
可见性 | android:visibility="" visiable可见、invisiable不可见但占位(点击无效)、gone不可见不占位(点击无效)。 |
不强制变成大写字母 | android:textAllCaps="false" |
权重, | android:weightSum="" 定义了weight总和的最大值 |
二、TextView
字体加粗 | android:textStyle="bold" |
尾部省略号 | android:ellipsize="end" |
2.1 类中字符串拼接AS提醒问题
AS提示:
-
不使用Number.toString() 格式的数字,它不会正确地处理分数分隔符和特定于地区的数字。考虑使用规范格式(%d或%f)的字符串来代替。
-
不要通过字符串文字(例如:“你好”)来显示文本,硬编码的文本不能被正确地翻译成其他语言。考虑使用Android资源字符串。
-
不要通过连接建立消息文本块,这样的信息不能被正确的翻译。
解决办法:
-
直接拼接
tv.text = getString(R.string.userName).plus(userBean.userName)
-
穿插拼接:%+数字 表示第几个参数,$s字符串数值、$d整数数值、$f浮点数值。
//string.xml
<string name="current_date">当前日期:%1$d年%2$d月%3$d日</string>
//kt
tv.text = String.format(context.getString(R.string.current_date),2023,06,14)
三、Button
关闭英文大写 | android:textAllCaps="false" |
四、ImageView
代码设置图片资源 | iv.setInageResource(R.drawable.pic) |
五、EditText
设置提示文字 | android:hint="" |
随输入文字数量自动拉伸 | android:maxLines="" 超过后文字会自动向上滚动 |
六、AlertDialog
设置标题 | setTitle("") |
设置内容 | setMessage("") |
确认按钮 | setPositiveButton("") { dialog,which -> } |
取消按钮 | setNegativeButton("") { dialog,which -> } |
返回键关闭 | setCancelable(false) |
显示 | show() |
AlertDialog.Builder(this).apply {
setTitle("This is Dialog")
setMessage("Something important.")
setCancelable(false)
setPositiveButton("OK") { dialog, which -> }
setNegativeButton("Cancel") { dialog, which -> }
show()
}
七、ProgressBar
检查/设置 可见性 | pb.visibility |
设置水平样式 | style="@android:style/Widget.ProgressBar.Horizontal" |
最大值 | android:max="" |
获取/设置 值 | pb.progress |
用自定义图片 | android:indeterminateDrawable="@drawable/..." |
八、EditText
输入框高度随着内容输入变大,设置minLines,把高度设为wrap_content。
背景默认显示的内容 | android:hint="" |
最小行数 | android:minLines="3" |
输入从顶端开始 | android:gravity="Top" |
隐藏下划线 | android:background="@null" |
android:focusedByDefault="false" |
获取光标位置 | getSelectionStart() //光标前 getSelectionEnd() //光标后 |
获取焦点不弹出键盘 | setShowSoftInputOnFocus(false) |
九、SrcollView
隐藏滚动条 | android:scrollbars="none" |
十、CoordinatorLayout
沉浸状态栏且布局不会向上偏移 | android:fitsSystemWindows="true" |
十一、AppBarLayout
去除阴影 | app:elevation="2dp" |
十二、CardView
圆角大小 | card_view:cardCornerRadius |
阴影大小,为0去除阴影 | app:cardElevation="0dp" |
阴影最大高度 | card_view:cardMaxElevation |
十三、TextInputLayout
默认hint颜色 | android:textColorHint="@color/white" |
悬浮的小hint颜色 | app:hintTextColor="@color/white" |
小眼睛 | app:passwordToggleEnabled="true" |
小眼睛颜色 | app:passwordToggleTint="@color/white" |
开启计数 | app:counterEnabled="true" |
最大位数 | app:counterMaxLength="15" |
计数的颜色 | app:counterTextColor="@color/black" |
默认下划线 | 主题中设置<item name="colorControlNormal">@color/white</item> |
选中后下划线 | 主题中设置<item name="colorControlActivated">@color/white</item> |
十四、ExpandableListView
子选项的背景色 | android:childDivider="@color/transparent" |
去除点击水波纹 | android:listSelector="@color/transparent" |
十五、GridLayout
列数 | android:columnCount="4" |
添加条目的动画 | android:animateLayoutChanges="true" |
十六、CardView
因为cardview自动阴影,设置elevation能改变FrameLayout里面显示的顺序,有阴影的时候,将不会遵循默认的自动覆盖逻辑,elevation最大的值会在最上层。
十七、ImageButton
如果是纯图片并且带连接的话,这种情况就可以使用Imagebutton(图片是重点),如果图片只是起到一个背景效果,则可以使用Button,它有很多Textview 的属性可以使用。
十九、ListView
取消分割线 | android:dividerHeight="0dp" android:divider="@android:color/transparent" |
取消滚动条 | android:scrollbars="none" |
二十、SurfaceView
用来显示动态的画面(相机、游戏、mp4)。内部维护了一个双缓存机制,A线程加载数据的时候B显示,B线程加载数据的时候A显示,所以初始化慢,可以直接在子线程中更新UI。
二十一、VideoView
VedioView vv = findViewById(R.id.vv);
vv.setVideoPath("http://baidu.com/hello.mp4");
vv.start();
二十二、VieGroup
添加子view的默认动画 | android:animateLayoutChanges="true" |
二十三、CheckBox
改变勾选前后的样式 | android:button="@drawable/xxx_selector" |
android:button="@null" android:drawableLeft="@drawable/xxx_selector" |