首页 文章 精选 留言 我的

精选列表

搜索[文旅营销和支付],共10000篇文章
优秀的个人博客,低调大师

andorid 修改字体一文搞定

本文首发于公众号“AntDream”,欢迎微信搜索“AntDream”或扫描文章底部二维码关注,和我一起每天进步一点点 替换字体也是一个比较常见的需求,一般分几种情况。实现起来也不麻烦,这里简单记录下 全局替换字体 步骤1 assets目录下拷贝字体文件 步骤2 application中替换默认字体 在Application的onCreate方法中增加替换方法 /** * 设置自定义字体 * * @param context * @param staticTypefaceFieldName 需要替换的系统字体样式 * @param fontAssetName 替换后的字体样式 */ public static void setDefaultFont(Context context, String staticTypefaceFieldName, String fontAssetName) { // 根据路径得到Typeface Typeface regular = Typeface.createFromAsset(context.getAssets(), fontAssetName); // 设置全局字体样式 replaceFont(staticTypefaceFieldName, regular); } private static void replaceFont(String staticTypefaceFieldName, final Typeface newTypeface) { try { final Field staticField = Typeface.class.getDeclaredField(staticTypefaceFieldName); staticField.setAccessible(true); //替换系统字体样式 staticField.set(null, newTypeface); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } 步骤3:新增主题Theme,并在AndroidManifest文件中注册 新增主题 <style name="YourTheme" parent="AppTheme.Base"> <item name="android:typeface" >serif</item> </style> 在AndroidManifest.xml文件中设置主题 <application xmlns:tools="http://schemas.android.com/tools" ... android:theme="@style/YourTheme" tools:replace="android:theme"> ... </application> 替换某些布局中的字体,也就是局部替换 步骤1:在res目录下新建font目录,拷贝字体文件 步骤2: 代码中替换 TextView textView = (TextView) findViewById(R.id.textView_font); Typeface typeface = ResourcesCompat.getFont(this, R.font.myfont); textView.setTypeface(typeface); 欢迎关注我的微信公众号,和我一起每天进步一点点!

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

一文看懂ConstraintLayout的用法

ConstraintLayout 相对于 RelativeLayout来说性能更好,布局上也更加灵活。在最新的Google Android开发文档中是推荐使用 ConstraintLayout的,下面来看看具体用法。 0x00 相对位置(Relative positioning) 这个比较简单,看图解释,假设控件B要放在控件A的右侧,可以使用 layout_constraintLeft_toRightOf属性。 <Button android:id="@+id/buttonA" ... /> <Button android:id="@+id/buttonB" ... app:layout_constraintLeft_toRightOf="@+id/buttonA" /> 看图2可以了解控件约束属性代表的含义。 类似相对位置的约束属性有: layout_constraintLeft_toLeftOf layout_constraintLeft_toRightOf layout_constraintRight_toLeftOf layout_constraintRight_toRightOf layout_constraintTop_toTopOf layout_constraintTop_toBottomOf layout_constraintBottom_toTopOf layout_constraintBottom_toBottomOf layout_constraintBaseline_toBaselineOf layout_constraintStart_toEndOf layout_constraintStart_toStartOf layout_constraintEnd_toStartOf layout_constraintEnd_toEndOf 0x01 外边距(Margins) 这个属性也好理解,看图3 可以通过以下属性设置一个控件相对另一个控件的外边距: android:layout_marginStart android:layout_marginEnd android:layout_marginLeft android:layout_marginTop android:layout_marginRight android:layout_marginBottom 属性值必须是大于或者等于0。 接一下看一个 RelativeLayout 没有的属性: 0x02 Margins when connected to a GONE widget 当一个相对的控件隐藏时, ConstraintLayout也可以设置一个不同的边距: layout_goneMarginStart layout_goneMarginEnd layout_goneMarginLeft layout_goneMarginTop layout_goneMarginRight layout_goneMarginBottom 具体的栗子下面会讲到。 0x03 Centering positioning and bias 居中以及设置偏差 <android.support.constraint.ConstraintLayout ...> <Button android:id="@+id/button" ... app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"/> </android.support.constraint.ConstraintLayout> 还可以设置bias属性,表示子控件相对父控件的位置倾向,可以使用属性: layout_constraintHorizontal_bias layout_constraintVertical_bias 假设设置控件A相对父控件横向偏差是30%: <android.support.constraint.ConstraintLayout ...> <Button android:id="@+id/button" ... app:layout_constraintHorizontal_bias="0.3" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"/> </android.support.constraint.ConstraintLayout> 0x04 弧形定位(Circular positioning) 这个属性是在1.1版本添加的。 可以使用属性有: layout_constraintCircle: 相对控件的id layout_constraintCircleRadius: 相对控件中心的距离,也就是圆的半径 layout_constraintCircleAngle: 相对夹角 (从 0 ~ 360度) 例如,图6代码示例 <Button android:id="@+id/buttonA" ... /> <Button android:id="@+id/buttonB" ... app:layout_constraintCircle="@+id/buttonA" app:layout_constraintCircleRadius="100dp" app:layout_constraintCircleAngle="45" /> 0x05 Visibility behavior 一般情况下,设置 GONE属性后,控件就不会出现在布局中了,B控件对A控件的margin属性也就没有作用了。 但是 ConstraintLayout 能对已经设置 GONE属性的控件进行特殊处理。当A控件设置 GONE之后,A控件相当于变成了一个点,B控件相对于对A的约束仍然是起作用的。图7的代码示例,A控件设置成了 GONE,当B控件的 margin属性还是有作用的。 <android.support.constraint.ConstraintLayout ...> <Button android:id="@+id/buttonA" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button A" android:visibility="gone" app:layout_constraintLeft_toLeftOf="parent" /> <!--当A控件设置Gone之后,B控件的margin属性是起作用的,即左边距还是30dp--> <Button android:id="@+id/buttonB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="30dp" android:text="button B" app:layout_constraintLeft_toRightOf="@+id/buttonA" /> </android.support.constraint.ConstraintLayout> 然而有时候,B控件是不希望相对于隐藏控件的属性还起作用。这时候可以用到上面0x02提到的 goneMargin属性。 <android.support.constraint.ConstraintLayout ...> <Button android:id="@+id/buttonA" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button A" android:visibility="gone" app:layout_constraintLeft_toLeftOf="parent" /> <!--当A控件设置Gone之后,希望B控件的左边距为0dp,那么可以设置layout_goneMarginLeft属性--> <Button android:id="@+id/buttonB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="30dp" android:text="button B" app:layout_goneMarginLeft="0dp" app:layout_constraintLeft_toRightOf="@+id/buttonA" /> </android.support.constraint.ConstraintLayout> 0x06 尺寸约束(Dimensions constraints) 设置最小或最大尺寸 可以使用以下属性: android:minWidth android:minHeight android:maxWidth android:maxHeight 当 ConstraintLayout宽高设置为 wrap_content时,以上属性可以起作用。 设置百分比布局 当 ConstraintLayout 子布局的宽或高设置为0dp时,可以对宽或高设置百分比,例如设置一个按钮的宽是屏幕宽度的30%,那么可以这样处理: <android.support.constraint.ConstraintLayout ...> <!--按钮width属性设置为0dp,然后需要指定layout_constraintWidth_default,以及layout_constraintWidth_percent两个属性--> <Button android:id="@+id/buttonB" android:layout_width="0dp" android:layout_height="wrap_content" android:text="button B" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintWidth_default="percent" app:layout_constraintWidth_percent="0.3" /> </android.support.constraint.ConstraintLayout> 设置宽度百分比布局: layout_width或者layout_height设置为0dp 设置layout_constraintWidth_default="percent"或者layout_constraintHeight_default="percent" 通过layout_constraintWidth_percent或者layout_constraintHeight_percent指定百分比 设置宽高比例 当 layout_width或者 layout_height设置为0dp时,还可以通过 layout_constraintDimensionRatio设置宽高比例。该比例表示 width:height的值。 <Button android:layout_width="wrap_content" android:layout_height="0dp" app:layout_constraintDimensionRatio="1:1" /> 当 layout_width与 layout_height都设置为0dp时,通过 app:layout_constraintDimensionRatio 指定宽高的比例。这时控件的宽高将按照该比例相对于父布局的大小设置宽高。 <android.support.constraint.ConstraintLayout ...> <Button android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintDimensionRatio="h,16:9" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> h,16:9的含义是 h:w=16:9 也可设置 w,9:16效果是一样的。 0x07 Chains 在横轴或或者数轴上的控件相互约束时,可以组成一个链式约束。 图9中,A控件与B控件相互约束,这就是一个简单的链式约束。 链头 Chain Style 可以通过 layout_constraintHorizontal_chainStyle或 layout_constraintVertical_chainStyle设置链式控件的样式。这个属性有点像 LinearLayout中的 weight 属性平分布局。 CHAIN_SPREAD Weighted chain CHAIN_SPREAD_INSIDE CHAIN_PACKED 设置权重 layout_constraintHorizontal_weight layout_constraintVertical_weight 0x08 引用 https://developer.android.com/reference/android/support/constraint/ConstraintLayout

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

一文读懂 with ... as 语句的来龙去脉

说到 with 大家通常看到的应该是这样的: 示例 1 with open('courses.txt') as f: for i in f: print(i.strip()) 打开一个文件,然后循环做一些事情。但是你知道为什么会有 with 吗?我们自己是不是能够写出可以作用在 with 关键字上的对象呢? 现在,我们带着上述两个问题来说一说 with 的由来以及上下文管理器相关内容。 with 语句的目的是简化 try/finally 模式。这种模式用于保证一段代码运行完毕后执行某项操作,即便那段代码由于异常、return 语句或sys.exit() 调用而中止,也会执行指定的操作。finally 子句中的代码通常用于释放重要的资源,或者还原临时变更的状态。 示例1的功能我们可以使用 try/finally 模式实

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

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

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册