Android Studio 开发工具类、样式、其余的配置

  • 一、工具类
    • 1.Log输出
    • 2.轻量存储SharedPreferences
  • 二、主题与样式
    • 1.主题
    • 2.常用样式
      • 1.按钮圆角背景
      • 2.按钮圆角边框
      • 3.布局右下方阴影
      • 4.EditText 下划线
  • 三、AVD设备
    • 1.自定义分辨率设备

一、工具类

1.Log输出

增强型Log输出工具类

package xxx;import android.text.TextUtils;
import android.util.Log;/*** 增强型Log工具,提取自XUtils工具集,强化了输出功能,可以输出长字符串* 自定义TAG输出格式:customTagPrefix:className.methodName(L:lineNumber)*/
public class LogEx {public static boolean isDebug = true;public static String customTagPrefix = "X_LOG";private static final int stringBuffer = 2000;private LogEx() {}private static String generateTag() {StackTraceElement caller = new Throwable().getStackTrace()[2];String tag = "%s.%s(L:%d)";String callerClazzName = caller.getClassName();callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());tag = TextUtils.isEmpty(customTagPrefix) ? tag : customTagPrefix + ":" + tag;return tag;}public static void d(String content) {if (!isDebug) return;String tag = generateTag();printLog("d", tag, content, null);}public static void d(String content, Throwable tr) {if (!isDebug) return;String tag = generateTag();printLog("d", tag, content, tr);}public static void e(String content) {if (!isDebug) return;String tag = generateTag();printLog("e", tag, content, null);}public static void e(String content, Throwable tr) {if (!isDebug) return;String tag = generateTag();printLog("e", tag, content, tr);}public static void i(String content) {if (!isDebug) return;String tag = generateTag();printLog("i", tag, content, null);}public static void i(String content, Throwable tr) {if (!isDebug) return;String tag = generateTag();printLog("i", tag, content, tr);}public static void v(String content) {if (!isDebug) return;String tag = generateTag();printLog("v", tag, content, null);}public static void v(String content, Throwable tr) {if (!isDebug) return;String tag = generateTag();printLog("v", tag, content, tr);}public static void w(String content) {if (!isDebug) return;String tag = generateTag();printLog("w", tag, content, null);}public static void w(String content, Throwable tr) {if (!isDebug) return;String tag = generateTag();printLog("w", tag, content, tr);}public static void w(Throwable tr) {if (!isDebug) return;String tag = generateTag();printLog("w", tag, null, tr);}public static void wtf(String content) {if (!isDebug) return;String tag = generateTag();printLog("wtf", tag, content, null);}public static void wtf(String content, Throwable tr) {if (!isDebug) return;String tag = generateTag();printLog("wtf", tag, content, tr);}public static void wtf(Throwable tr) {if (!isDebug) return;String tag = generateTag();printLog("wtf", tag, null, tr);}private static void printLog(String type, String tag, String content, Throwable tr) {long length = content.length();if (length < stringBuffer || length == stringBuffer) {switchLog(type, tag, content, tr);} else {while (content.length() > stringBuffer) {String logContent = content.substring(0, stringBuffer);content = content.replace(logContent, "");switchLog(type, tag, logContent, tr);}switchLog(type, tag, content, tr);}}private static void switchLog(String type, String tag, String content, Throwable tr) {switch (type.trim().toLowerCase()) {case "i":Log.i(tag, content, tr);break;case "v":Log.v(tag, content, tr);break;case "d":Log.d(tag, content, tr);break;case "e":Log.e(tag, content, tr);break;case "w":Log.w(tag, content, tr);break;case "wtf":Log.wtf(tag, content, tr);default:Log.i(tag, content, tr);}}
}

使用方式类似于Log类,加入了自定义的 X_LOG 前缀

LogEx.i(">>>>>>> onStart MainActivity screen size:" + 2400 + " " + 936);
//打印:I/X_LOG:MainActivity.onCreate(L:178): >>>>>>> onStart MainActivity screen size:2400 936

2.轻量存储SharedPreferences

其中使用到了MyApp.content,其实就是指向一个Context,也可以在构造时作为参数传递给工具类

MyApp.class 内容举例

public class MyApp extends Application {public static Context context;@Overridepublic void onCreate() {super.onCreate();context = getApplicationContext();}
}

SpUtils 工具类如下

package xxx;import android.content.Context;
import android.content.SharedPreferences;
import xxx.MyApp;/** 轻量存储工具类SharedPreferences */
public class SpUtils {private SharedPreferences sp;private SharedPreferences.Editor editor;/** 常量的形式保存KEY在此类里面 */public static final String USER_NAME = "username";private SpUtils(String name) {sp = MyApp.context.getSharedPreferences(name, Context.MODE_PRIVATE);editor = sp.edit();}/** 单例模式 */private static SpUtils instance;public static SpUtils getInstance(String name) {if (instance == null) {synchronized (SpUtils.class) {if (instance == null) {instance = new SpUtils(name);}}}return instance;}/** String */public void putString(String spName, String value) {editor.putString(spName, value);editor.commit();}public String getString(String spName, String defaultvalue) {return sp.getString(spName, defaultvalue);}public String getString(String spName) {return sp.getString(spName, "");}/** Int */public void putInt(String spName, int value) {editor.putInt(spName, value);editor.commit();}public int getInt(String spName, int defaultvalue) {return sp.getInt(spName, defaultvalue);}/** Float */public void putFloat(String key, float value) {editor.putFloat(key, value);editor.commit();}public float getFloat(String key, float defValue) {return sp.getFloat(key, defValue);}/** Long */public void putLong(String key, long value) {editor.putLong(key, value);editor.commit();}public long getLong(String key, long defValue) {return sp.getLong(key, defValue);}/** Boolean */public void putBoolean(String key, boolean value) {editor.putBoolean(key, value);editor.commit();}public boolean getBoolean(String key, boolean defValue) {return sp.getBoolean(key, defValue);}/** 查看SP文件中是否存在此 key */public boolean hasExists(String fileName, String key) {return sp.contains(key);}/** 清空SP里所有数据 */public void clear() {editor.clear();editor.commit();}/** 删除SP里指定key对应的数据项 */public void remove(String key) {editor.remove(key);editor.commit();}
}

简单使用一下,例如:清空账号与密码缓存时的操作

// 重新登录时,缓存密码清空
SpUtils.getInstance("userinfo").putString("password", "");
SpUtils.getInstance("userinfo").putString("account", "");

二、主题与样式

1.主题

对于主题的介绍,我们来看以下博主们的精美文章吧,介绍的都很不错,我先划下水

https://blog.csdn.net/geduo_83/article/details/86561559

https://blog.csdn.net/l707941510/article/details/93772581

https://blog.csdn.net/xchaha/article/details/79047140

2.常用样式

1.按钮圆角背景

在 drawable 目录下新建 btn_radius_red.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="@color/colorPrimaryDark"/><corners android:topLeftRadius="10dip"android:topRightRadius="10dip"android:bottomRightRadius="10dip"android:bottomLeftRadius="10dip" />
</shape>

使用:

android:background="@drawable/btn_radius_red"

<Buttonstyle="@style/text_12_white"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginStart="@dimen/length_10"android:layout_marginEnd="@dimen/length_10"android:layout_weight="1"android:background="@drawable/btn_radius_red"android:minHeight="@dimen/length_30"android:text="保 存" />

效果:

2.按钮圆角边框

在 drawable 目录下新建 btn_border_red.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><corners android:radius="5dp" /><strokeandroid:width="1dp"android:color="@color/colorPrimary" /><solid android:color="@color/transparent" /><paddingandroid:bottom="@dimen/length_2"android:left="@dimen/length_2"android:right="@dimen/length_2"android:top="@dimen/length_2" />
</shape>

使用:

android:background="@drawable/btn_border_red"

<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="@dimen/length_3"android:background="@drawable/btn_border_red"android:insetTop="@dimen/length_1"android:insetBottom="@dimen/length_1"android:minHeight="@dimen/length_25"android:text="OK"android:textColor="@color/colorPrimaryDark"android:textSize="@dimen/text_size_9" />

效果:

3.布局右下方阴影

在 drawable 目录下新建 layout_shadow_right.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"><item><shape android:shape="rectangle"><paddingandroid:bottom="2dp"android:left="0dp"android:right="2dp"android:top="0dp" /><solid android:color="#0DCCCCCC" /><corners android:radius="8dp" /></shape></item><item><shape android:shape="rectangle"><paddingandroid:bottom="2dp"android:left="0dp"android:right="2dp"android:top="0dp" /><solid android:color="#10CCCCCC" /><corners android:radius="8dp" /></shape></item><item><shape android:shape="rectangle"><paddingandroid:bottom="2dp"android:left="0dp"android:right="2dp"android:top="0dp" /><solid android:color="#15CCCCCC" /><corners android:radius="8dp" /></shape></item><item><shape android:shape="rectangle"><paddingandroid:bottom="2dp"android:left="0dp"android:right="2dp"android:top="0dp" /><solid android:color="#20CCCCCC" /><corners android:radius="8dp" /></shape></item><item><shape android:shape="rectangle"><paddingandroid:bottom="2dp"android:left="0dp"android:right="2dp"android:top="0dp" /><solid android:color="#30CCCCCC" /><corners android:radius="8dp" /></shape></item><item><shape><solid android:color="#FFFFFF" /><corners android:radius="4dp" /></shape></item>
</layer-list>

使用:

作用在 layout 布局上

android:background="@drawable/layout_shadow_right"

效果:

4.EditText 下划线

这个可以在 themes 文件中加入 style

<!-- 横线输入框样式-->
<style name="EditTextLine" parent="Theme.AppCompat.Light"><item name="colorControlNormal">@color/white</item><item name="colorControlActivated">@color/purple_200</item>
</style>

使用:

作用在 EditText 编辑框控件中

android:theme="@style/EditTextLine"

效果:

三、AVD设备

1.自定义分辨率设备

假设我要显示在一个 横屏 1920x1080 6 英寸的设备上,那么就可以打开 AVD Manager 进行添加

1.首先要新建一个 名为 Plus6 的 Hardware Profile


2.创建完成之后,我们需要选择该硬件并进行下一步


3.下一步就要选择该设备的 Android 版本了


4.然后基本创建完毕,我们可以最后命名或者调整一下横竖屏切换


5.现在我们去我们的 xml 布局页面去预览一下吧,有时候选项没有最新添加的这个设备那么就需要重启下软件即可


Android开发工具类、样式、一些配置相关推荐

  1. Android开发工具类集合

    各种帮助类汇总:https://github.com/Blankj/AndroidUtilCode 常用的 ios 风格 dialog 和 meterial design 风格的 dialog:htt ...

  2. Android开发工具类 Utils

    包括了各种工具类.辅助类.管理类等 Awesome_API: https://github.com/marktony/Awesome_API/blob/master/Chinese.md 收集中国国内 ...

  3. Android开发工具类

    包括了各种工具类.辅助类.管理类等 Awesome_API: https://github.com/marktony/Awesome_API/blob/master/Chinese.md 收集中国国内 ...

  4. android httputils更换成https请求,Android开发工具类之HttpUtils

    今天我们讲常用的开发工具类之HttpUtils,我发现上两次,我对于每个方法都进行了一定的解释,有人跟我评论和留言说,不用我解释,这么简单,这么明显的使用方法,再笨的人也能看懂,多此一举,好吧,这次我 ...

  5. Android开发工具类集锦

    概述 本人做android开发已有段日子了,在开发的过程中一直使用着工具类,包括别人已经封装好的工具类以及自己封装的工具类,本篇博客向大家介绍自己在开发过程中经常使用到的一些简单实用的工具类,在文章末 ...

  6. Android开发工具下载及环境配置

    工作一年,家里出了点事,回家发展,找到一份Android开发,大恩大德不嫌弃我菜,而且公司规模在本地来说同行业内是比较大的了,挺好的,今天刚来,发现电脑没安装开发软件,遂着手先把吃饭的工具先下下来,难 ...

  7. Android 开发工具类 13_ SaxService

    网络 xml 解析方式 1 package com.example.dashu_saxxml; 2 3 import java.io.IOException; 4 import java.io.Inp ...

  8. android 开发工具类,Android中常用开发工具类—持续更新...

    一.自定义ActionBar public class ActionBarTool { public static void setActionBarLayout(Activity act,Conte ...

  9. 分享一个整理了很多Android开发工具类的链接AndroidUtilCode

    https://github.com/Blankj/AndroidUtilCode API Activity相关→ActivityUtils.java→Demo isActivityExists : ...

最新文章

  1. C++friend 友元类和友元函数
  2. Cloud Native 介绍
  3. 三层神经网络实现手写字母的识别(基于tensorflow)
  4. Java注解的Retention和RetentionPolicy
  5. PAT乙级 1022 D进制的A+B
  6. 关于原生AJAX和jQueryAJAX的编程
  7. 林业局计算机考试试题,汕头市农业局林业局计算机信息网络安全保护管理制度...
  8. unity 裙子摆动_Unity中实现MMD效果
  9. linux微信登陆失败,微信登陆失败原因与解决方法
  10. idea remote debug
  11. 【jq练习】基本选择器
  12. C# WPF MVVM 实战 – 2.4 单元测试
  13. 网络爬虫-神器fiddler抓取app数据
  14. keep-alive:
  15. IBM bladecenter H刀箱BladeCenter北电交换机VLAN配置
  16. 项目挂到iis 点击导入 未将对象引用设置到对象的实例_用Notion管理读书项目
  17. 正则表达式-连续多位相同字符判断的正则表达式
  18. helper.exe
  19. 搭建S60手机端Python软件运行,开发,发布平台
  20. iar软件中C语言跳出for循环,关于 IAR一些C语言扩展

热门文章

  1. numpy 数组 ::_看起来不错,没有麻烦:使用NumPy进行数组编程
  2. “高性能Web开发技术”网上聊天活动
  3. 硬盘分区表的修复(Ubuntu安装盘的另类用法)
  4. 计算机应用专业社会环境分析,计算机应用专业人才岗位需求分析调研报告
  5. (转)再思人机智能融合
  6. 重试利器之Guava Retrying (一、介绍及简单实现)
  7. 无法创建目录或文件问题的解决办法
  8. 虚拟与现实的结合 大众全新一代途锐带我走进了“星际战场”
  9. mac地址修改_【干货分享】交换机工作基础——MAC地址表的构成与安全
  10. echarts 虚实结合的折线图