
我正在尝试更改textvIEws,复选框,按钮和android.support.design.Widget.TextinputLayout的默认字体.为了清楚说明,我设置了“ androID:FontFamily”> cursive<.如下所示,它似乎在Android Studio预览中正确显示,但在模拟器中却不正确.另请注意,密码(提示)似乎在这两种方式中均不起作用.非常感谢您的帮助,以强调为什么会这样.
styles.xml:
<!-- Base application theme. --><style name="Apptheme" parent="theme.AppCompat.light.DarkActionbar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="androID:textcolor">#ed328b</item> <item name="androID:TitleTextcolor">#ed328b</item> <item name="androID:textSize">14sp</item> <item name="androID:FontFamily">cursive</item></style><style name="TextLabel" parent="Widget.Design.TextinputLayout"> <!-- Hint color and label color in FALSE state --> <item name="androID:textcolorHint">#ed328b</item> <item name="androID:textSize">14sp</item> <!-- Label color in TRUE state and bar color FALSE and TRUE State --> <item name="colorAccent">#ed328b</item> <item name="androID:textcolor">#ed328b</item> <item name="colorControlnormal">#ed328b</item> <item name="colorControlActivated">#ed328b</item> <item name="androID:textcolorSecondary">#ed328b</item> <item name="androID:textcolorPrimary">#ed328b</item> <item name="androID:FontFamily">cursive</item></style>activity_main.xml:
<androID.support.design.Widget.TextinputLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:theme="@style/TextLabel" androID:layout_margintop="@dimen/space_2"> <EditText androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_marginBottom="10dp" androID:inputType="textEmailAddress" androID:hint="@string/signin_emailaddress" androID:ID="@+ID/txtEmail" /> </androID.support.design.Widget.TextinputLayout> <androID.support.design.Widget.TextinputLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:theme="@style/TextLabel" androID:layout_margintop="@dimen/space_2"> <EditText androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_marginBottom="10dp" androID:inputType="textPassword" androID:hint="@string/signin_password" androID:ID="@+ID/txtPassword" /> </androID.support.design.Widget.TextinputLayout> <CheckBox androID:ID="@+ID/rememberBox" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="5dp" androID:layout_gravity="start" androID:text="remember my email and password" androID:textAlignment="center" androID:textcolor="#ed328b" androID:textSize="11dp" />解决方法:
AndroID不支持通过XML将自定义字体直接应用于文本小部件的内置支持.
您可以参考本文档以设置xml中的字体:https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html
或者您需要在Java代码中设置字体以@R_669_6502@.
或者,您可以执行以下 *** 作:
第一
您需要定义自己的样式.在您的/ res / values文件夹中,打开/创建attrs.xml文件,并添加一个可声明样式的对象,如下所示:
<?xml version="1.0" enCoding="utf-8"?><resources> <declare-styleable name="FontText"> <attr name="typefaceAsset" format="string"/> </declare-styleable></resources>第二
假设您想经常使用此小部件,则应该为加载的Typeface对象设置一个简单的缓存,因为从内存中动态加载它们会花费一些时间.就像是:
public class FontManager { private static FontManager instance; private AssetManager mgr; private Map<String, Typeface> Fonts; private FontManager(AssetManager _mgr) { mgr = _mgr; Fonts = new HashMap<String, Typeface>(); } public static voID init(AssetManager mgr) { instance = new FontManager(mgr); } public static FontManager getInstance() { if (instance == null) { // App.getContext() is just one way to get a Context here // getContext() is just a method in an Application subclass // that returns the application context AssetManager assetManager = App.getContext().getAssets(); init(assetManager); } return instance; } public Typeface getFont(String asset) { if (Fonts.containsKey(asset)) return Fonts.get(asset); Typeface Font = null; try { Font = Typeface.createFromAsset(mgr, asset); Fonts.put(asset, Font); } catch (Exception e) { } if (Font == null) { try { String fixedAsset = fixAssetfilename(asset); Font = Typeface.createFromAsset(mgr, fixedAsset); Fonts.put(asset, Font); Fonts.put(fixedAsset, Font); } catch (Exception e) { } } return Font; } private String fixAssetfilename(String asset) { // Empty Font filename? // Just return it. We can't help. if (TextUtils.isEmpty(asset)) return asset; // Make sure that the Font ends in '.ttf' or '.ttc' if ((!asset.endsWith(".ttf")) && (!asset.endsWith(".ttc"))) asset = String.format("%s.ttf", asset); return asset; }}这将允许您使用.ttc文件扩展名,但尚未经过测试.
第三
创建一个新类来继承TextVIEw.此特定示例考虑了已定义的XML字体(粗体,斜体等),并将其应用于字体(假设您使用的是.ttc文件).
/** * TextVIEw subclass which allows the user to define a truetype Font file to use as the vIEw's typeface. */public class FontText extends TextVIEw { public FontText(Context context) { this(context, null); } public FontText(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FontText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); if (isInEditMode()) return; TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FontText); if (ta != null) { String FontAsset = ta.getString(R.styleable.FontText_typefaceAsset); if (!TextUtils.isEmpty(FontAsset)) { Typeface tf = FontManager.getInstance().getFont(FontAsset); int style = Typeface.norMAL; float size = getTextSize(); if (getTypeface() != null) style = getTypeface().getStyle(); if (tf != null) setTypeface(tf, style); else Log.d("FontText", String.format("Could not create a Font from asset: %s", FontAsset)); } } }}最后终于
用完全限定的类名替换XML中的TextVIEw实例.就像使用AndroID名称空间一样声明您的自定义名称空间.请注意,“ typefaceAsset”应指向/ assets目录中包含的.ttf或.ttc文件.
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:custom="http://schemas.androID.com/apk/res-auto" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <com.example.FontText androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="This is a custom Font text" custom:typefaceAsset="Fonts/AvenirNext-Regular.ttf"/></relativeLayout> 总结 以上是内存溢出为你收集整理的字体在Android Studio预览中更改,但在模拟器/设备中未更改全部内容,希望文章能够帮你解决字体在Android Studio预览中更改,但在模拟器/设备中未更改所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)