android在xml中定义图片数组

其实图片在android 中的应用都是integer的id

<?xml version="1.0" encoding="utf-8"?><resources>    <integer-array name="bottom_list">        <item>@mipmap/perm_group_messagesitem>        <item>@mipmap/perm_group_phone_callsitem>        <item>@mipmap/perm_group_system_clockitem>        <item>@mipmap/perm_group_system_toolsitem>        <item>@mipmap/perm_group_accountsitem>    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中添加图片

  1. 在project工程目录新建ScreenShot目录,添加图片一同push到github
    Github 实例

  2. 打开github上相关路径到图片,点击raw获取路径
    Android 开发中的一些小技巧_第1张图片

  3. 打开记事本暂时存储路径
    图片地址

和我们写md博客添加图片类似,前面需要加上![],中括号内是描述,()内是url地址和 别名

Activity背景透明

<resources>        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">        -- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary        "colorPrimaryDark">@color/colorPrimaryDark        "colorAccent">@color/colorAccent    style>    <style name="AppTheme.NoActionBar">        <item name="windowActionBar">falseitem>        --是否显示title-->        <item name="windowNoTitle">trueitem>    style>    <style name="AppTheme.NoActionBar.Trans">        --是否浮现在activity之上-->        <item name="android:windowIsFloating">trueitem>        --Window 的背景-->        <item name="android:windowBackground">@android:color/transparent                "android:windowIsTranslucent">true                "android:windowAnimationStyle">@android:style/Animation.Translucent                "android:windowContentOverlay">@null                "android:backgroundDimEnabled">false    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      "android:windowContentOverlay">@null      "android:windowIsTranslucent">false      "android:windowAnimationStyle">@null      "android:windowDisablePreview">true      "android:windowNoDisplay">false  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          "android:windowNoTitle">true          "android:windowFullscreen">true          "android:windowContentOverlay">@null   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/CustomActionBarstyle>

然后在ActionBar的样式中通过android:titleTextStyle定义标题的样式
副标题文字样式可以通过android:subtitleTextStyle来指定

菜单的样式,比如在Light主题下,想要menu的文字颜色是白色的,怎么做呢?
需要指定属性android:actionMenuTextColor

ps:

    -- Base application theme. -->    

防止Android ScrollView 子View 获取焦点自动滚动

在ScrollView中嵌套的子View获取焦点或者布局改变的时候会自动滚动到焦点View,针对这个问题,我们可以这样解决

    • 让界面顶部的某一个View获取focus
    • 让获取焦点的View失去焦点view.setFocusable(false);
    • 手动scrollto(0,0)
  1. 或者重写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 layoutparamsLinearLayout.LayoutParams dividerParams =new LinearLayout.LayoutParams(w, 0);mDivider.setLayoutParams(dividerParams);

参考:AbsListView的Header不能直接固定大小为0

更多相关文章

  1. 图片切换
  2. Android下载网络图片到本地
  3. android 图片叠加效果
  4. android中获取网络图片
  5. android 自定义progressbar 样式

随机推荐

  1. Android 动画效果 --Animation
  2. IPC机制
  3. Android 第三方库使用实例——编写.so库
  4. Android噪音检测系统
  5. 【Android】Activity Window WMS源码关系
  6. Android(安卓)IPC进程间通讯机制
  7. Android基础入门教程——8.1.2 Android中
  8. Soloπ:支付宝开源的Android专项测试工具
  9. Android中的对话框的使用技巧
  10. Android第六个功能:XmlPullParser解析XML