android在xml中定义图片数组
其实图片在android 中的应用都是integer的id
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="bottom_list">
<item>@mipmap/perm_group_messages</item>
<item>@mipmap/perm_group_phone_calls</item>
<item>@mipmap/perm_group_system_clock</item>
<item>@mipmap/perm_group_system_tools</item>
<item>@mipmap/perm_group_accounts</item>
</integer-array>
</resources>
java代码获取
TypedArray imgs = getResources().obtainTypedArray(R.array.bottom_list);
for (int i = 0; i < imgs.length(); ++i) {
// index ,defaultValue
int drawableResId = imgs.getResourceId(i,R.mipmap.perm_group_system_tools);
}
imgs.recycle();
image.setImageResource(drawableResId);
android获取随机字符串
利用查表法,和随机数法
// 定义字符数组
private static char[] chars = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N','O','P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
//获取随机字符串
public static String getRandomString() {
Random random = new Random();
//获取随机长度
int length = random.nextInt(chars.length) + 1;
char[] data = new char[length];
for (int i = 0; i < length; i++) {
//获取随机字符
int index = random.nextInt(chars.length);
data[i] = chars[index];
}
return new String(data);
}
github push 项目README.md中添加图片
在project工程目录新建ScreenShot目录,添加图片一同push到github
打开github上相关路径到图片,点击raw获取路径
打开记事本暂时存储路径
和我们写md博客添加图片类似,前面需要加上![],中括号内是描述,()内是url地址和 别名
Activity背景透明
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style>
<style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <!--是否显示title--> <item name="windowNoTitle">true</item> </style>
<style name="AppTheme.NoActionBar.Trans"> <!--是否浮现在activity之上--> <item name="android:windowIsFloating">true</item> <!--Window 的背景--> <item name="android:windowBackground">@android:color/transparent</item> <!--是否透明--> <item name="android:windowIsTranslucent">true</item> <!--activity 进出场动画--> <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item> <!--是否有遮盖--> <item name="android:windowContentOverlay">@null</item> <!--背景是否变暗--> <item name="android:backgroundDimEnabled">false</item> </style>
</resources>
在Manifest中添加theme
<activity android:name=".SencondActivity" android:theme="@style/AppTheme.NoActionBar.Trans" />
巧用android:windowBackground设置启动背景
在启动应用的时候,首个activity会做一些初始化操作,就会导致有一个黑屏的时期或者过渡不自然。通过上面theme的方式就可以很巧妙的避免这一点
<style name="AppTheme.NoActionBar.NoDisplay" parent="@android:Theme"> <item name="android:windowBackground">@null</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowIsTranslucent">false</item> <item name="android:windowAnimationStyle">@null</item> <item name="android:windowDisablePreview">true</item> <item name="android:windowNoDisplay">false</item> </style>
<activity android:name=".SencondActivity" android:theme="@style/AppTheme.NoActionBar.NoDisplay" />
这是一种不显示的方式。也就是取消默认的window preview
还有一种设置启动图片的方式
<style name="AppTheme.NoActionBar.CustomBackground" > <!--显示loading的图片--> <item name="android:windowBackground">@drawable/loading</item> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> <item name="android:windowContentOverlay">@null</item> </style>
Mac下adb环境配置
-
command+space
打开terminal.app
,输入cd ~
- 输入
touch .bash_profile
回车,这个命令是 创建或者 更新.bash_profile
文件 - 输入
open -e .bash_profile
,打开.bash_profile
文件 - 此时如果环境变量为空,则会打开空文本文件,其中添加如下内容即可。
export PATH=${PATH}:/Users/bobomee/Library/Android/sdk/platform-tools
其中/Users/bobomee/Library/Android/sdk/为sdk路径。
自定义ActionBar标题和菜单文字颜色
标题和菜单是acationBar的一部分,所以首先定义ActionBar的样式style
<style name="AppTheme" parent="AppBaseTheme"> <item name="android:actionBarStyle">@style/CustomActionBar</item> </style>
然后在ActionBar的样式中通过android:titleTextStyle
定义标题的样式
副标题文字样式可以通过android:subtitleTextStyle
来指定
菜单的样式,比如在Light主题下,想要menu的文字颜色是白色的,怎么做呢?
需要指定属性android:actionMenuTextColor
ps:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/main_color</item>
<item name="colorPrimaryDark">@color/main_color</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@color/main_background</item>
<item name="@android:attr/actionBarSize">50dp</item>
<item name="actionMenuTextColor">@color/white</item>
</style>
防止Android ScrollView 子View 获取焦点自动滚动
在ScrollView中嵌套的子View获取焦点或者布局改变的时候会自动滚动到焦点View,针对这个问题,我们可以这样解决
-
- 让界面顶部的某一个View获取
focus
- 让获取焦点的View失去焦点
view.setFocusable(false)
; - 手动
scrollto(0,0)
- 让界面顶部的某一个View获取
或者重写ScrollView中的
computeScrollDeltaToGetChildRectOnScreen
,让该方法返回0
@Override
protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
return 0;
}
Android drawable与text居中对齐的TextView
【Android】自定义控件让TextView的drawableLeft与文本一起居中显示
其后在github上看到了xesam/CenterDrawableTextView,可以直接使用,非常方便
EditTex密码的显示与隐藏
经常有这样的需求,在密码输入框的后面,为了让用户看到输入的内容,这是会显示一个小眼睛在输入框的后面,用于明文或者密文显示
//password明文密文切换
public static void visible(EditText editText, boolean show) {
editText.setTypeface(Typeface.DEFAULT);
editText.setTransformationMethod(show ? HideReturnsTransformationMethod.getInstance()
: PasswordTransformationMethod.getInstance());
int start = editText.getSelectionStart();
int end = editText.getSelectionEnd();
editText.setSelection(start, end);
}
ListView HeaderView 高度设置为0
我们知道给ListView
设置HeaderView
必须在setAdapter
之前,这时候可以通过LayoutParams设置HeaderView的宽高.但是如果设置为0,则会转变为wrap_content
正确的做法是通过代码添加一层View来包裹HeaderView,之后就可以设置了
LinearLayout mWrapper1 = new LinearLayout(activity);
mDivider = LayoutInflater.from(activity)
.inflate(R.layout.list_divider_layout, mWrapper1, false);
mWrapper1.addView(mDivider);
listView.addHeaderView(mWrapper1);
//set layoutparams
LinearLayout.LayoutParams dividerParams =
new LinearLayout.LayoutParams(w, 0);
mDivider.setLayoutParams(dividerParams);