首页 文章 精选 留言 我的

精选列表

搜索[JavaScript库],共10000篇文章
优秀的个人博客,低调大师

Android 图解创建外部 lib 库及自定义 View

随着插件化/组件化的快速发展,现在大部分的项目开发中都会提取公共的代码制作成 Library module,根据具体的业务需求进行拆分。小菜也学习一下如何拆分 lib 包,实际操作很简单,整理一下操作步骤。 拆分创建 Library (1) 在当前 Project 下,File -> New Module,选择 Android Library,进行下一步; (2) 设置具体的 Library/Module/Package 等名称,注意:Module 名称与 Library 相匹配默认为小写,需要的话手动调整,进行下一步; (3) 此时在当前 Project 中就已经创建好 Library; (4) 在当前 Project 的 settings.gradle 中就会自动生成创建的 Module; Tips: :myview 中的 : 代表的与 app 同级目录下的 Module。 在当前 app 的 build.gradle 中 dependencies{} 中添加 implementation project(':myview') 即可正常接入。 自定义 View 小菜在新建的 Library 中添加一个自定义按钮,可以添加配置图标和文字以及背景样式。因为只是为了测试 Library Module,所以功能很简单,实现方式也很简单,只是几个基本控件的组合。小菜只是简单的整理一下。 新建一个 MyView 继承自 RelativeLayout,实现基本的构造方法; 在构造方法中实现对布局的添加,控件的绑定以及一些基本的 setXX 方法; 至此 MyView 就可以应用,但所有但属性都需要通过 setXX 方法来设置;这当然是不合理的,于是小菜新建一个 attrs 文件,在资源文件中设置基本的样式,并在 MyView 的 obtainAttributes 方法中逐一绑定即可; <?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <declare-styleable name="my_view" tools:ignore="MissingDefaultResource"> <!-- 中间文字颜色 --> <attr name="tv_color" format="color" /> <!-- 中间文字显隐性 --> <attr name="tv_show" format="boolean" /> <!-- 中间文字内容 --> <attr name="tv_str" format="string" /> <!-- 中间文字大小 --> <attr name="tv_size" format="float" /> <!-- 右侧文字颜色 --> <attr name="right_tv_color" format="color" /> <!-- 右侧文字显隐性 --> <attr name="right_tv_show" format="boolean" /> <!-- 右侧文字内容 --> <attr name="right_tv_str" format="string" /> <!-- 右侧文字大小 --> <attr name="right_tv_size" format="float" /> <!-- 整体背景颜色 --> <attr name="bg_color" format="color" /> <!-- 整体边框颜色 --> <attr name="strok_color" format="color" /> <!-- 整体边框圆角 --> <attr name="bg_radius" format="float" /> <!-- 中间图片显隐性 --> <attr name="iv_show" format="boolean" /> <!-- 中间图片资源 --> <attr name="iv_src" format="reference" /> </declare-styleable> </resources> 至此,MyView 自定义按钮以及完成,在 app 中也是正常调用即可。 public class MyView extends RelativeLayout { private Context mContext; private RelativeLayout mRlay; private ImageView mIv; private TextView mTv, mRightTv; GradientDrawable drawable = new GradientDrawable(); int mTvColor, mRightTvColor, mRlayBgColor, mStrokeColor, mIvSrc; boolean isTvShow, isRightTvShow, isIvShow; float mTvSize, mRightTvSize, mRadiusSize; String mTvStr, mRightTvStr; public MyView(Context context) { super(context); mContext = context; initView(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; initView(); obtainAttributes(context,attrs); } private void initView() { LayoutInflater.from(mContext).inflate(R.layout.my_view_btn, this,true); mRlay = findViewById(R.id.my_view_rly); mIv = findViewById(R.id.my_view_iv); mTv = findViewById(R.id.my_view_tv); mRightTv = findViewById(R.id.my_view_rtv); } private void obtainAttributes(Context context, AttributeSet attrs) { TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.my_view); mTvColor = ta.getColor(R.styleable.my_view_tv_color, Color.BLACK); mTv.setTextColor(mTvColor); mRightTvColor = ta.getColor(R.styleable.my_view_right_tv_color, Color.BLACK); mRightTv.setTextColor(mRightTvColor); mRlayBgColor = ta.getColor(R.styleable.my_view_bg_color, Color.WHITE); mRlay.setBackgroundColor(mRlayBgColor); mStrokeColor = ta.getColor(R.styleable.my_view_strok_color, Color.BLACK); isIvShow = ta.getBoolean(R.styleable.my_view_iv_show, true); mIv.setVisibility(isIvShow?View.VISIBLE:View.GONE); isRightTvShow = ta.getBoolean(R.styleable.my_view_right_tv_show, true); mRightTv.setVisibility(isRightTvShow?View.VISIBLE:View.GONE); isTvShow = ta.getBoolean(R.styleable.my_view_tv_show, true); mTv.setVisibility(isTvShow?View.VISIBLE:View.GONE); mTvSize = ta.getFloat(R.styleable.my_view_tv_size, 16.0f); mTv.setTextSize(mTvSize); mRightTvSize = ta.getFloat(R.styleable.my_view_right_tv_size, 14.0f); mRightTv.setTextSize(mRightTvSize); mRadiusSize = ta.getFloat(R.styleable.my_view_bg_color, 80.0f); drawable = (GradientDrawable) getResources().getDrawable(R.drawable.user_login_corner_qq); drawable.setCornerRadius(mRadiusSize); drawable.setStroke(1, mStrokeColor); drawable.setColor(mRlayBgColor); mRlay.setBackground(drawable); mTvStr = ta.getString(R.styleable.my_view_tv_str); mTv.setText(mTvStr); mRightTvStr = ta.getString(R.styleable.my_view_right_tv_str); mRightTv.setText(mRightTvStr); mIvSrc = ta.getResourceId(R.styleable.my_view_iv_src, R.mipmap.user_login_icon_qq); mIv.setImageResource(mIvSrc); ta.recycle(); } public void setMyViewTv(String textStr) { mTv.setText(textStr); } public void setMyViewTvColor(int color) { mTv.setTextColor(color); } public void setMyViewTvSize(float size) { mTv.setTextSize(size); } public void isMyViewTvShow(boolean state) { mTv.setVisibility(state ? View.VISIBLE : View.GONE); } public void setMyViewIv(Drawable drawable) { mIv.setImageDrawable(drawable); } public void isMyViewIvShow(boolean state) { mIv.setVisibility(state ? View.VISIBLE : View.GONE); } public void isMyViewRightTvShow(boolean state) { mRightTv.setVisibility(state ? View.VISIBLE : View.GONE); } public void setMyViewRightTvText(String textStr) { mRightTv.setText(textStr); } public void setMyViewRightTvSize(float size) { mRightTv.setTextSize(size); } public void setMyViewRightTvColor(int color) { mRightTv.setTextColor(color); } public void setMyViewBgColor(int color) { drawable.setColor(color); mRlay.setBackground(drawable); } public void setMyViewBgRadius(float radius) { drawable.setCornerRadius(radius); mRlay.setBackground(drawable); } public void setMyViewBgStrokeColor(int color) { drawable.setStroke(1, color); mRlay.setBackground(drawable); } public void setMyViewBgDrawable(Drawable drawable) { mRlay.setBackground(drawable); } } Tips: attrs.xml 中如果需要用到资源文件,可以使用 format="reference",代表某一个资源ID。 小菜也是初步尝试,有不对的地方烦请提醒。以下是小菜公众号,欢迎闲来吐槽~

优秀的个人博客,低调大师

Java 常用类库 之 Random 随机数类实例

http://www.verejava.com/?id=169931132381103 /** 知识点:Random 随机数类洗牌 */ import java.util.*; public class TestRandomCard { public static void main(String[] args) { String[] cards = { "A", "A", "A", "A", "2", "2", "2", "2", "3", "3", "3", "3", "4", "4", "4", "4", "5", "5", "5", "5", "6", "6", "6", "6", "7", "7", "7", "7", "8", "8", "8", "8", "9", "9", "9", "9", "10", "10", "10", "10", "J", "J", "J", "J", "Q", "Q", "Q", "Q", "K", "K", "K", "K", "小王", "大王" }; System.out.println("原牌:"); for (int i = 0; i < cards.length; i++) { System.out.print(cards[i] + ","); } //随机打乱 Set set = new HashSet(); Random rn = new Random(); while (true) { set.add(rn.nextInt(54)); if (set.size() == 54) { break; } } System.out.println("\n54个随机索引号:"); Object[] iter = set.toArray(); for (int i = 0; i < iter.length; i++) { System.out.print(iter[i] + ","); } } } http://www.verejava.com/?id=169931132381103

优秀的个人博客,低调大师

Java 常用类库 之 基本类型的包装类

http://www.verejava.com/?id=17159727064934 public class Test { public static void main(String[] args) { // 8 种基本数据类型对应的包装类 // byte Byte // short Short // int Integer // long Long // float Float // double Double // char Character // boolean Boolean // // 装箱和拆箱 // 装箱: 将一个基本数据类型转换成包装类 new Integer(int i) // 拆箱: 将一个包装类转换成基本数据类型 int Integer.intValue(); int i = 100; Integer integer = new Integer(i);//装箱 System.out.println(i); System.out.println(integer); int i2 = integer.intValue();//拆箱 System.out.println(i2); // int Integer.parseInt(String s) String str = "200"; int i3 = Integer.parseInt(str); System.out.println(i3); // String Integer.toString(int i) int i4 = 300; String str2 = Integer.toString(i4); System.out.println(str2); // 3. Integer Integer.valueOf(String s) String str3 = "400"; Integer integer2 = Integer.valueOf(str3); System.out.println(integer2); } } http://www.verejava.com/?id=17159727064934

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册