当前位置: 首页 > news >正文

做网站首页布局设计注意事项以营销网建为

做网站首页布局设计注意事项,以营销网建为,公众号平台编辑,腾讯云和wordpress版权声明#xff1a;本文为博主原创文章#xff0c;未经博主允许不得转载。https://www.jianshu.com/p/a08d754944c4 转载请标明出处#xff1a;https://www.jianshu.com/p/a08d754944c4 本文出自 AWeiLoveAndroid的博客 【前言】过年回家忙着干活#xff0c;忙着给亲戚的孩… 版权声明本文为博主原创文章未经博主允许不得转载。https://www.jianshu.com/p/a08d754944c4 转载请标明出处https://www.jianshu.com/p/a08d754944c4 本文出自 AWeiLoveAndroid的博客 【前言】过年回家忙着干活忙着给亲戚的孩子发红包好累忙里偷闲打开studio看了一下v4包之前看过几个类这次是每个类都看了一下原来Android的v4包的源码也有一些是写的不是那么友好的还有很多改善空间。 下面就拿其中的android.support.v4.text这个包里面的 TextUtilsCompat 和 TextUtilsCompatJellybeanMr1 这两个类来做一个具体讲解。 本文源码同步发布在github详情请点击 https://github.com/AweiLoveAndroid/refactor-android-support-v4 一、首先看一下Androidv4包下面的 TextUtilsCompat 和 TextUtilsCompatJellybeanMr1 源码 一TextUtilsCompat 源码 public final class TextUtilsCompat {private static class TextUtilsCompatImpl {TextUtilsCompatImpl() {}NonNullpublic String htmlEncode(NonNull String s) {StringBuilder sb new StringBuilder();char c;for (int i 0; i s.length(); i) {c s.charAt(i);switch (c) {case :sb.append(lt;); //$NON-NLS-1$break;case :sb.append(gt;); //$NON-NLS-1$break;case :sb.append(amp;); //$NON-NLS-1$break;case \://http://www.w3.org/TR/xhtml1// The named character reference apos; (the apostrophe, U0027) was// introduced in XML 1.0 but does not appear in HTML. Authors should// therefore use #39; instead of apos; to work as expected in HTML 4// user agents.sb.append(#39;); //$NON-NLS-1$break;case :sb.append(quot;); //$NON-NLS-1$break;default:sb.append(c);}}return sb.toString();}public int getLayoutDirectionFromLocale(Nullable Locale locale) {if (locale ! null !locale.equals(ROOT)) {final String scriptSubtag ICUCompat.maximizeAndGetScript(locale);if (scriptSubtag null) return getLayoutDirectionFromFirstChar(locale);// This is intentionally limited to Arabic and Hebrew scripts, since older// versions of Android platform only considered those scripts to be right-to-left.if (scriptSubtag.equalsIgnoreCase(ARAB_SCRIPT_SUBTAG) ||scriptSubtag.equalsIgnoreCase(HEBR_SCRIPT_SUBTAG)) {return ViewCompat.LAYOUT_DIRECTION_RTL;}}return ViewCompat.LAYOUT_DIRECTION_LTR;}/*** Fallback algorithm to detect the locale direction. Rely on the first char of the* localized locale name. This will not work if the localized locale name is in English* (this is the case for ICU 4.4 and Urdu script)** param locale* return the layout direction. This may be one of:* {link ViewCompat#LAYOUT_DIRECTION_LTR} or* {link ViewCompat#LAYOUT_DIRECTION_RTL}.** Be careful: this code will need to be updated when vertical scripts will be supported*/private static int getLayoutDirectionFromFirstChar(NonNull Locale locale) {switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) {case Character.DIRECTIONALITY_RIGHT_TO_LEFT:case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:return ViewCompat.LAYOUT_DIRECTION_RTL;case Character.DIRECTIONALITY_LEFT_TO_RIGHT:default:return ViewCompat.LAYOUT_DIRECTION_LTR;}}}private static class TextUtilsCompatJellybeanMr1Impl extends TextUtilsCompatImpl {TextUtilsCompatJellybeanMr1Impl() {}OverrideNonNullpublic String htmlEncode(NonNull String s) {return TextUtilsCompatJellybeanMr1.htmlEncode(s);}Overridepublic int getLayoutDirectionFromLocale(Nullable Locale locale) {return TextUtilsCompatJellybeanMr1.getLayoutDirectionFromLocale(locale);}}private static final TextUtilsCompatImpl IMPL;static {final int version Build.VERSION.SDK_INT;if (version 17) { // JellyBean MR1IMPL new TextUtilsCompatJellybeanMr1Impl();} else {IMPL new TextUtilsCompatImpl();}}/*** Html-encode the string.* param s the string to be encoded* return the encoded string*/NonNullpublic static String htmlEncode(NonNull String s) {return IMPL.htmlEncode(s);}/*** Return the layout direction for a given Locale** param locale the Locale for which we want the layout direction. Can be null.* return the layout direction. This may be one of:* {link ViewCompat#LAYOUT_DIRECTION_LTR} or* {link ViewCompat#LAYOUT_DIRECTION_RTL}.** Be careful: this code will need to be updated when vertical scripts will be supported*/public static int getLayoutDirectionFromLocale(Nullable Locale locale) {return IMPL.getLayoutDirectionFromLocale(locale);}public static final Locale ROOT new Locale(, );static String ARAB_SCRIPT_SUBTAG Arab;static String HEBR_SCRIPT_SUBTAG Hebr;private TextUtilsCompat() {} }二TextUtilsCompatJellybeanMr1 源码 /*** Jellybean MR1 - specific TextUtils API access.*/ RequiresApi(17) TargetApi(17) class TextUtilsCompatJellybeanMr1 {NonNullpublic static String htmlEncode(NonNull String s) {return TextUtils.htmlEncode(s);}public static int getLayoutDirectionFromLocale(Nullable Locale locale) {return TextUtils.getLayoutDirectionFromLocale(locale);} }二、分析一下以上源码的一些需要改进的问题仅个人理解如有不同意见欢迎提出 通过以上源码来看看起来确实有点不是很舒服。 一排列顺序有点乱我格式化了一下如下看的稍微清楚了一些 /*** 格式化之后的TextUtilsCompat类*/ public class TextUtilsCompat {private static final TextUtilsCompatImpl IMPL;public static final Locale ROOT new Locale(, );static String ARAB_SCRIPT_SUBTAG Arab;static String HEBR_SCRIPT_SUBTAG Hebr;private TextUtilsCompat() {}static {final int version Build.VERSION.SDK_INT;if (version 17) { // JellyBean MR1IMPL new TextUtilsCompatJellybeanMr1Impl();} else {IMPL new TextUtilsCompatImpl();}}/*** Html-encode the string.* param s the string to be encoded* return the encoded string*/NonNullpublic static String htmlEncode(NonNull String s) {return IMPL.htmlEncode(s);}/*** Return the layout direction for a given Locale** param locale the Locale for which we want the layout direction. Can be null.* return the layout direction. This may be one of:* {link ViewCompat#LAYOUT_DIRECTION_LTR} or* {link ViewCompat#LAYOUT_DIRECTION_RTL}.** Be careful: this code will need to be updated when vertical scripts will be supported*/public static int getLayoutDirectionFromLocale(Nullable Locale locale) {return IMPL.getLayoutDirectionFromLocale(locale);}private static class TextUtilsCompatImpl {TextUtilsCompatImpl() {}NonNullpublic String htmlEncode(NonNull String s) {StringBuilder sb new StringBuilder();char c;for (int i 0; i s.length(); i) {c s.charAt(i);switch (c) {case :sb.append(lt;); //$NON-NLS-1$break;case :sb.append(gt;); //$NON-NLS-1$break;case :sb.append(amp;); //$NON-NLS-1$break;case \://http://www.w3.org/TR/xhtml1// The named character reference apos; (the apostrophe, U0027) was// introduced in XML 1.0 but does not appear in HTML. Authors should// therefore use #39; instead of apos; to work as expected in HTML 4// user agents.sb.append(#39;); //$NON-NLS-1$break;case :sb.append(quot;); //$NON-NLS-1$break;default:sb.append(c);}}return sb.toString();}public int getLayoutDirectionFromLocale(Nullable Locale locale) {if (locale ! null !locale.equals(ROOT)) {final String scriptSubtag ICUCompat.maximizeAndGetScript(locale);if (scriptSubtag null) {return getLayoutDirectionFromFirstChar(locale);}// This is intentionally limited to Arabic and Hebrew scripts, since older// versions of Android platform only considered those scripts to be right-to-left.if (scriptSubtag.equalsIgnoreCase(ARAB_SCRIPT_SUBTAG) ||scriptSubtag.equalsIgnoreCase(HEBR_SCRIPT_SUBTAG)) {return ViewCompat.LAYOUT_DIRECTION_RTL;}}return ViewCompat.LAYOUT_DIRECTION_LTR;}/*** Fallback algorithm to detect the locale direction. Rely on the first char of the* localized locale name. This will not work if the localized locale name is in English* (this is the case for ICU 4.4 and Urdu script)** param locale* return the layout direction. This may be one of:* {link ViewCompat#LAYOUT_DIRECTION_LTR} or* {link ViewCompat#LAYOUT_DIRECTION_RTL}.** Be careful: this code will need to be updated when vertical scripts will be supported*/private static int getLayoutDirectionFromFirstChar(NonNull Locale locale) {switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) {case Character.DIRECTIONALITY_RIGHT_TO_LEFT:case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:return ViewCompat.LAYOUT_DIRECTION_RTL;case Character.DIRECTIONALITY_LEFT_TO_RIGHT:default:return ViewCompat.LAYOUT_DIRECTION_LTR;}}}private static class TextUtilsCompatJellybeanMr1Impl extends TextUtilsCompatImpl {TextUtilsCompatJellybeanMr1Impl() {}OverrideNonNullpublic String htmlEncode(NonNull String s) {return TextUtilsCompatJellybeanMr1.htmlEncode(s);}Overridepublic int getLayoutDirectionFromLocale(Nullable Locale locale) {return TextUtilsCompatJellybeanMr1.getLayoutDirectionFromLocale(locale);}}}二TextUtilsCompat 这个类里面有两个内部类一个是TextUtilsCompatImpl一个是TextUtilsCompatJellybeanMr1Impl,TextUtilsCompatJellybeanMr1Impl是继承自TextUtilsCompatImpl的。 三从静态代码块看出api 大于17 使用 new TextUtilsCompatJellybeanMr1Impl(); api小于17 使用TextUtilsCompatImpl。TextUtilsCompatTextUtilsCompatImpl和TextUtilsCompatJellybeanMr1Impl里面都有 htmlEncode 方法和 getLayoutDirectionFromLocale 方法。 静态代码块里面通过TextUtilsCompatImpl IMPL这个常量来判断当api大于17用TextUtilsCompatJellybeanMr1Impl否则用TextUtilsCompatImpl然后htmlEncode方法调用了对应内部类里面的htmlEncode方法getLayoutDirectionFromLocale调用了对应内部类里面的getLayoutDirectionFromLocale方法。 四TextUtilsCompatImpl和TextUtilsCompatJellybeanMr1Impl里面都有 htmlEncode 方法和 getLayoutDirectionFromLocale 方法看看它们的区别。 1TextUtilsCompatJellybeanMr1Impl这个内部类的方法解析 htmlEncode(NonNull String s) 方法 返回的是TextUtilsCompatJellybeanMr1.htmlEncode(s); 调用了TextUtils.htmlEncode(s);getLayoutDirectionFromLocale(Nullable Locale locale) 方法返回的是TextUtilsCompatJellybeanMr1.getLayoutDirectionFromLocale(locale); 调用了TextUtils.getLayoutDirectionFromLocale(locale);2TextUtilsCompatImpl这个内部类的方法解析 htmlEncode(NonNull String s) 方法 返回的是在这个方法内部写了一遍跟TextUtils.htmlEncode(s);方法里面的一模一样。getLayoutDirectionFromLocale(Nullable Locale locale) 方法返回的是重新写了一遍这个方法是真正有所区别的地方。三、根据我做过项目用到的MVP的开发模式我把共同的htmlEncode方法和getLayoutDirectionFromLocale方法抽取出一个接口然后分别用两个实现类去实现接口然后用TextUtilsCompat这个类去判断调用哪个实现类的方法这样看起来更直观一些。具体步骤如下 一抽取公共接口ITextUtilsCompat /*** 抽取公用的接口*/ public interface ITextUtilsCompat {/*** Html-encode the string.* param s the string to be encoded* return the encoded string*/public String htmlEncode(NonNull String s);/*** Return the layout direction for a given Locale** param locale the Locale for which we want the layout direction. Can be null.* return the layout direction. This may be one of:* {link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_LTR} or* {link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_RTL}.** Be careful: this code will need to be updated when vertical scripts will be supported*/public int getLayoutDirectionFromLocale(Nullable Locale locale); }二写一个TextUtilsCompatJellybeanMr1实现ITextUtilsCompat 接口然后把之前TextUtilsCompatJellybeanMr1类里面的复制过来具体如下 /*** 兼容Android 17 版本* Jellybean MR1 - specific TextUtils API access.*/ RequiresApi(17) TargetApi(17) class TextUtilsCompatJellybeanMr1 implements ITextUtilsCompat{OverrideNonNullpublic String htmlEncode(NonNull String s) {return TextUtils.htmlEncode(s);}Overridepublic int getLayoutDirectionFromLocale(Nullable Locale locale) {return TextUtils.getLayoutDirectionFromLocale(locale);} }三写一个类TextUtilsCompatImpl实现ITextUtilsCompat 接口然后把之前TextUtilsCompat类里面的有关代码复制过来具体如下 /*** Android 17以下版本使用这个类*/ class TextUtilsCompatImpl implements ITextUtilsCompat{public static final Locale ROOT new Locale(, );static String ARAB_SCRIPT_SUBTAG Arab;static String HEBR_SCRIPT_SUBTAG Hebr;OverrideNonNullpublic String htmlEncode(NonNull String s) {StringBuilder sb new StringBuilder();char c;for (int i 0; i s.length(); i) {c s.charAt(i);switch (c) {case :sb.append(lt;); //$NON-NLS-1$break;case :sb.append(gt;); //$NON-NLS-1$break;case :sb.append(amp;); //$NON-NLS-1$break;case \://http://www.w3.org/TR/xhtml1// The named character reference apos; (the apostrophe, U0027) was// introduced in XML 1.0 but does not appear in HTML. Authors should// therefore use #39; instead of apos; to work as expected in HTML 4// user agents.sb.append(#39;); //$NON-NLS-1$break;case :sb.append(quot;); //$NON-NLS-1$break;default:sb.append(c);}}return sb.toString();}Overridepublic int getLayoutDirectionFromLocale(Nullable Locale locale) {if (locale ! null !locale.equals(ROOT)) {final String scriptSubtag ICUCompat.maximizeAndGetScript(locale);if (scriptSubtag null) {return getLayoutDirectionFromFirstChar(locale);}// This is intentionally limited to Arabic and Hebrew scripts, since older// versions of Android platform only considered those scripts to be right-to-left.if (scriptSubtag.equalsIgnoreCase(ARAB_SCRIPT_SUBTAG) ||scriptSubtag.equalsIgnoreCase(HEBR_SCRIPT_SUBTAG)) {return ViewCompat.LAYOUT_DIRECTION_RTL;}}return ViewCompat.LAYOUT_DIRECTION_LTR;}/*** Fallback algorithm to detect the locale direction. Rely on the first char of the* localized locale name. This will not work if the localized locale name is in English* (this is the case for ICU 4.4 and Urdu script)** param locale* return the layout direction. This may be one of:* {link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_LTR} or* {link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_RTL}.** Be careful: this code will need to be updated when vertical scripts will be supported*/private static int getLayoutDirectionFromFirstChar(NonNull Locale locale) {switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) {case Character.DIRECTIONALITY_RIGHT_TO_LEFT:case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:return ViewCompat.LAYOUT_DIRECTION_RTL;case Character.DIRECTIONALITY_LEFT_TO_RIGHT:default:return ViewCompat.LAYOUT_DIRECTION_LTR;}} }四封装TextUtilsCompat给调用者使用具体如下 /*** v4包下面的TextUtilsCompat的简单优化* 这里使用的是策略模式根据不同api版本调用不同的接口实现类* 这样写更好维护。*/ public final class TextUtilsCompat {private static final ITextUtilsCompat IMPL;private TextUtilsCompat() {}static {final int version Build.VERSION.SDK_INT;// JellyBean MR1 大于等于17if (version Build.VERSION_CODES.JELLY_BEAN_MR1) {IMPL new TextUtilsCompatJellybeanMr1();} else {IMPL new TextUtilsCompatImpl();}}/*** Html-encode the string.* param s the string to be encoded* return the encoded string*/NonNullpublic static String htmlEncode(NonNull String s) {return IMPL.htmlEncode(s);}/*** Return the layout direction for a given Locale** param locale the Locale for which we want the layout direction. Can be null.* return the layout direction. This may be one of:* {link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_LTR} or* {link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_RTL}.** Be careful: this code will need to be updated when vertical scripts will be supported*/public static int getLayoutDirectionFromLocale(Nullable Locale locale) {return IMPL.getLayoutDirectionFromLocale(locale);}}到此TextUtilsCompat 这个类就封装完了。看完之后是不是很清爽其实还有很多类似的类都可以根据类似的方式做一下改进的。源码不是完美的只要掌握以上示例代码的思想还是很容易的让代码更好理解更简洁清晰的。 【好消息】我的微信公众号正式开通了关注一下吧 关注一下我的公众号吧
http://www.sadfv.cn/news/143709/

相关文章:

  • 定制企业网站费用织梦网站必须下载
  • 怎么做直播网站刷弹幕地方门户网站的分类
  • 响应式网站优点崇明建设小学网站
  • 如何开一家网站建设公司?手机网站开发标准
  • 网站建设的分工国产做性直播视频网站
  • 保利建设开发总公司网站工商营业执照网上申报
  • 网站建设是广告么企业微信网站怎么做的
  • 佛山cms建站系统网络安全的重要性
  • 给别人做网站挣钱么国内对企业网站开发的研究
  • jeecms做企业网站网站建设系统网站自助建站系统
  • 无锡网站开发电话wordpress腾讯云储存
  • 网站制作目标及要求wordpress自适应模板
  • 建设一个企业网站爱情动做网站推荐
  • 优购物官方网站女鞋中石油网站建设
  • 石家庄做网站seo保定企业建网站
  • 整站seo包年费用手工活外发加工无押金
  • 影响网站权重的因素有哪些毕业设计ppt答辩模板
  • 做微商卖产品在哪个网站销量能好点临沂网站设计
  • 凡科可以建设多个网站吗snippets wordpress
  • 网站制作公司官网南京手机可怎么样做网站
  • 昆明市建设厅网站中山 灯饰 骏域网站建设专家
  • 网站服务器上线后要怎么做网站更换域名备案吗
  • 建站代理电子商务网站建设规划方案论文
  • 有哪些可以做图的网站啊建站公司走量渠道
  • 湖北省建设信息港网站商业网站建设费用
  • dede旅游网站个人网站用什么建站程序
  • ajax做网站iis怎么做ip网站吗
  • 网站设置在设备之间共享什么意思牧风的在wordpress
  • 广州 flash 网站做网站优化的工资有多高
  • 哪个网站可以做身份核验惠州做网站公司哪家好