杂七杂八的View使用总结
Button中的文字比TextView中的文字粗
保持统一的办法:为Button添加一下属性android:textAppearance="@android:style/Widget.TextView"
单行显示,超出使用...
符号展示
使用lines=1
实现:
android:lines="1"
android:ellipsize="end"
使用singleLine=true
实现:
android:singleLine="true"
android:ellipsize="end"
TextView的includeFontPadding属性说明
官方说明:Leave enough room for ascenders and descenders instead of using the font ascent and descent strictly. (Normally true).
个人总结:includeFontPadding可以帮助我们去除字体的上下内边距,即padding的值,但并不改变字体本身的高度!!!具体的在Android中字体高度的解释,请看下面的图文详解。
图文详解:引用地址
- Top - The maximum distance above the baseline for the tallest glyph in the font at a given text size.
- Ascent - The recommended distance above the baseline for singled spaced text.
- Descent - The recommended distance below the baseline for singled spaced text.
- Bottom - The maximum distance below the baseline for the lowest glyph in the font at a given text size.
- Leading - The recommended additional space to add between lines of text.
EditText限制输入字符
以输入手机号为例:限制EditText只能输入数字
方案1:不通用
edittext.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_VARIATION_NORMAL
方案2:
class PhoneInputFilter : InputFilter {
private val digits = "^[0-9]$"
override fun filter(source: CharSequence?, start: Int, end: Int, dest: Spanned?, dstart: Int, dend: Int): CharSequence {
if (source.isNullOrEmpty()) {
return ""
}
if (!isInputPhoneFormat(source)) {
return ""
}
return source
}
private fun isInputPhoneFormat(phone: CharSequence): Boolean {
return match(digits, phone)
}
private fun match(regex: String, str: CharSequence): Boolean {
if (str.isEmpty()) return false
val pattern = Pattern.compile(regex)
val matcher = pattern.matcher(str)
return matcher.matches()
}
}
//使用
edittext.filters = arrayOf(PhoneInputFilter())
EditText限制输入字符的长度
方案1:
edittext.filters = arrayOf(InputFilter.LengthFilter(24))
方案2:
<EditText
android:maxLength="24"
/>
EditText第二次覆盖掉输入框
复现设备:MI 3W,Android6.0.1
复现场景:加入购物车选择规格弹框(一般是半透明背景),选择数量EditText第二次点击会被软键盘覆盖掉。
复现代码:
- 自定义一个继承自Dialog(继承自DialogFragment也一样有问题)的Dialog:
public class CustomDialog extends Dialog {
public CustomDialog(@NonNull Context context) {
super(context);
}
public CustomDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
protected CustomDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_custom);
}
}
- 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:background="@color/colorAccent"
android:gravity="center"
android:inputType="number" />
</RelativeLayout>
只要EditText需要同时设置inputType属性和gravity属性,其中inputType属性值不为none,gravity属性值不为center_vertical,就能复现。
解决办法:将EditText的inputType属性改为numeric属性,但是需要注意的是numeric属性只支持数字展示。
ImageView的adjustViewBounds属性说明
官方说明:Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.
个人总结:
- 在布局中文件添加
android:adjustViewBounds="true"
语句,则该ImageView的ScaleType会被设置为fitCenter,除非再单独在布局文件中设置ScaleType属性,或者是在Java代码中设置ScaleType属性; - 只有在layout_width和layout_height其中之一是固定值,另外一个是wrap_content的时候,设置adjustViewBounds才有意义,比如设置layout_width的值为100dp,设置layout_height的值为wrap_content,那么就有以下两种情况:
2.1. 图片的宽度大于等于100dp:图片将等比例缩放到ImageView中。
2.2. 图片的宽度小于100dp:图片不会缩放,ImageView的高度等于图片的高度。
ViewGroup的clipToPadding属性说明
官方说明:true to clip children to the padding of the group, and resize (but not clip) any EdgeEffect to the padded region. False otherwise.
个人总结:以列表为例:true的时候,paddingTop部分不会跟随列表滚动,列表内容距离顶部具有固定的paddingTop值;false的时候,刚初始化页面之后,列表内容距离顶部的值为paddingTop,但是滚动的时候paddingTop会跟随列表滚动,此时列表内容距离顶部的距离就不是paddingTop了,可以想象为为列表添加一个Header之后的滚动效果。
适合场景:设置列表的第一个
(最后一个
)Item距离屏幕顶部
(底部
)有一段距离的情况。
ViewGroup的clipChildren属性说明
举个例子:比如说ViewPager中有RecyclerView,RecyclerView中每个Item的根布局是ConstraintLayout,ConstraintLayout中包含了一个ImageView。这个是ImageView想要超出ConstraintLayout展示就需要用到clipChildren属性,我们只需要为ImageView的爷爷布局添加android:clipChildren="false"
即可。
AlertDialog显示位置偏右
背景:我自己的小米测试机Android11,显示的DatePickerDialog(继承自AlertDialog)偏右,但是测试的OPPO测试机Android11显示正常
说明:有部分手机是会显示异常的
以下代码摘自:https://cache.one/read/2174057,实测有效
//省略builder构建代码
AlertDialog dialog = builder.create();
dialog.show();
//放在show()之后,不然有些属性是没有效果的,比如height和width
Window dialogWindow = dialog.getWindow();
WindowManager m = getWindowManager();
Display d = m.getDefaultDisplay(); // 获取屏幕宽、高
WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
// 设置宽度
p.width = (int) (d.getWidth() * 0.95); // 宽度设置为屏幕的0.95
p.gravity = Gravity.CENTER;//设置位置
dialogWindow.setAttributes(p);