1. tools:text使用
在开发android UI时,多使用tools:text进行显示,尽量不要使用android:text,使用tools:text需要在根标签添加 xmlns:tools="schemas.android.com/tools"
2. AS 常用快捷键
- ctrl+alt+o 清楚无用的import
- option+alt+L 格式化代码
- alt+enter 自动提示代码修改
- option+N 自动生成代码提示
- F2 可以快速定位到报红的位置
- shift + alt + 向上箭头 可以快速移动行到上一行
- command + L 可以将光标指定到具体的行
3. GradientDrawable 支持渐变色的Drawable
参考: Android开发 GradientDrawable详解
int[] colors = {Color.YELLOW, Color.GREEN, Color.BLUE};
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setColors(colors); //添加颜色组
gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);//设置线性渐变
gradientDrawable.setSize(50,50);
// gradientDrawable.setCornerRadius(AppUtil.dp2px(5));
// gradientDrawable.setOrientation(GradientDrawable.Orientation.TOP_BOTTOM);
mTextView.setBackground(gradientDrawable);
感悟,是时候好好学一下自定义view,自已画一些view玩玩了
4. 设置屏幕变暗
setAlpa(0.5f);
private void setAlpa(Float alpa) {
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.alpha = alpa;
getWindow().setAttributes(layoutParams);
}
5. 为啥同时不建议用RelativeLayout
# Android开发——LinearLayout和RelativeLayout的性能对比
ConstraintLayout,RelativeLayout和LinearLayout的性能对比
6. 自定义view
dp是对不同设备做适配的,自定义View用px
7. 为啥引用activity的static不会导致内存泄漏
static 的变量存储在常量区。可以直接应用
// 例如在TestActivity定义一个常量
public static final String a = "123";
public String a = "123";
// 在另一个activity可以直接引用
TestActivity.a;
// 如果不是static,需要new一个对象, 这里只是举例,实际中activity并不是new出来的
new TestActivity().b
8. textview getlinecount
staticLayout = new StaticLayout(item.getName(), poetryHolder.tvPoetryName.getPaint(), AppUtil.dp2px(96), Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
9 AS 刷新文件
当我们打包后,有时并不能立即看到apk文件,需要点击AS上的刷新按钮
10 java TextView设置加粗
public void setTitleBold(boolean isBold) {
if (mTitleTv != null) {
if (isBold) {// 加粗
mTitleTv.setTypeface(Typeface.DEFAULT_BOLD);
} else {// 常规
mTitleTv.setTypeface(Typeface.DEFAULT);
}
mTitleTv.invalidate();// 触发重绘
}
}
11 editText选中光标
edSearch.requestFocus();
参考:# android EditText搜索页面自动弹出软键盘,且响应软键盘的搜索按钮
12 androids自带返回键
override fun onBackPressed() {
super.onBackPressed()
}
13 Arouter调用
- build.app
plugins {
id 'kotlin-kapt'
}
defaultConfig {
kapt {
arguments {
arg("AROUTER_MODULE_NAME", project.getName())
}
}
}
dependencies 中加入
kapt 'com.alibaba:arouter-compiler:1.5.2'
被调用文件名上方
@Route(path = ARouterUtil.RouterMap.Login.HistoryLogin)
调用
ARouterUtil.INSTANCE.go(ARouterUtil.RouterMap.Login.HistoryLogin).navigation();
module需要被主模块app引用
arouter有缓存,删除app下的build重新编译
14 Arouter跳转错误的几个原因
- 看传输的data是否解析错误
- 是是否调用了.navigation()
- eventbus是否重复注册
15 使用Arouter实现startActivityForResult效果
Arouter调用.navigation时,可以传requestCode参数
16 android垂直view滚动
使用ViewFlipper(翻转视图),参考www.runoob.com/w3cnote/and…
17 自定义dialog铺满屏幕,没有左右间距
class CustomDialog(context: Context) : Dialog(context, R.style.dialog_no_background) {}
其中dialog_no_background定义在values的style文件中,如下
<resources>
<style name="dialog_no_background" parent="@android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
18 android中ListView或GridView出现在adapter中的getView出现多次position为0的情况
if (parent.getChildCount() == position) {
<span style="white-space:pre"> </span>//里面就是正常的position
}
else {
<span style="white-space:pre"> </span>//临时的position=0
}
19 adb输出日志
adb logcat > a.txt 可以输出所有的日志到a.txt,而且是实时的日志,就是日志太多了,文件会比较大
参考: # Androidstudio配置 adb 并将logcat日志导出到文件
20 Constraintlayout 新特性:Barrier、Group、Layer、Flow、ImageFilterView等
参考:Constraintlayout 新特性:Barrier、Group、Layer、Flow、ImageFilterView等
21 ConstraintLayout最详细使用
参考:# ConstraintLayout最详细使用,减少嵌套优化ui,提升app性能
22 ConstraintLayout中的子view实现左右居中且具有左右边距
左右居中很简单,
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
左右边距按说使用margin_left与margin_right就可以了,谁知不起作用
今天才知道原因
需要将宽度改成0dp,刚开始写成wrap_content
顺便说一下,match_parent是不起作用的
其实match_parent是起作用的,比如宽度设置为match_parent后,就不会再设置constraintLeft和constraintRight了
23 RecycleView复用机制导致显示混乱问题
解决:
- RecyclerView禁止复用,参考RecyclerView禁止复用
- 通过给adapter中的view设置tag,然后通过tag进行判读
imageViewGiftIcon.tag = "tag"
if (imageViewGiftIcon.tag == "tag") {
}
24 使用Progress时注意事项
进度条背景需使用
android:progressDrawable
不要使用
android:background
否则会出问题
25 fragment要有空的构造方法,或者使用FragmentFactory
参考:
27 Viewpager中的Fragment 滑动时保存状态的问题
参考 Viewpager中的Fragment 滑动时保存状态的问题
或者设置
// 4代表左右未显示的fragment,一共有2*4+1个fragment
viewPager.setOffscreenPageLimit(4);
28 ScrollView嵌套ViewPager显示不全问题,不能滑动问题
参考
关于apng与svga
apng是动图,android的ImageView可以正常显示
apng相比较svga动效比较大,svga小一点,都是可以显示动画效果的