一些可能有用的Android代码
2020年11月6日 更新View.outlineProvider
没地儿放它们。
渐隐重启Activity
1 | // 1 |
RingtoneManager
Note that the list of ringtones available will differ depending on whether the caller has the {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission.
1 | val intent = Intent(RingtoneManager.ACTION_RINGTONE_PICKER) |
onActivityResult
,
1 | val uri = data?.getParcelableExtra<Uri>(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) |
文件选择
需要READ_EXTERNAL_STORAGE
权限。
RecyclerView头部视差显示
使第一个View渐隐退出Window。
1 | list.addOnScrollListener(object :RecyclerView.OnScrollListener(){ |
ADB查看当前Activity
adb shell "dumpsys window w | grep name="
- 栈顶:
adb shell dumpsys activity | grep "mFocusedActivity"
- 栈顶Fragment:
adb shell dumpsys activity your.package.name
ADB截屏并保存
adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen.png
或者
adb exec-out screencap -p > screen.png
Grab Android screenshot to computer via ADB
让一个Activity有类似Android O或P彩蛋一样的背景。
设置theme为android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"
。
给Layout添加Ripple
创建
ripple_press.xml
1
2
3
4
5
6
7
8
9
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@android:color/white">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
</shape>
</item>
</ripple>在Layout中使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true">
<!-- ... -->
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="@drawable/ripple_press" />
<!-- ... -->
</com.google.android.material.card.MaterialCardView>clickable
和focusable
很重要,View
之前的View会有Ripple效果,之后的View则没有。
AutoClearedValue
带清除按钮的EditText
来自Best Practices for Using Text in Android (Google I/O’19)
1 | <LinearLayout |
Custom View中的onSaveInstanceState
Android didn’t manage the onSaveInstanceState/onRestoreInstanceState if no id is set to the view.
?attr, ?android:attr, ?colorPrimary, ?attr/colorPrimary…
https://developer.android.com/guide/topics/resources/providing-resources#ResourcesFromXml => Referencing style attributes`
File - Mark As Plain Text
重构时很有用,不会影响VS。
FragmentManager.setCustomAnimations()
‘s Parameters
@AnimatorRes @AnimRes int enter
: 应用到动画中进入屏幕的Fragment@AnimatorRes @AnimRes int exit
: 应用到动画中退出屏幕的Fragment@AnimatorRes @AnimRes int popEnter
: 应用到popBackStack
时动画中进入屏幕的Fragment@AnimatorRes @AnimRes int popExit
: 应用到popBackStack
时动画中退出屏幕的Fragment
Kotlin in
and out
1 | // 一个RecyclerView.ViewHolder子类的List |
clipToPadidng
Enables our items to still be drawn within the parent's padding.
View.outlineProvider
1 | binding.imageView.run { |