最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Android中动态设置字符的属性和样式style

Android crifan 7174浏览 0评论

【背景】

对于Android中的字符的属性设置,已经知道可以静态的在xml中去定义。

现在希望实现,可以动态的,在代码中去设置属性和样式。

【解决过程】

1.搜:

android set text style

找到:

java – Android – set TextView TextStyle programmatically? – Stack Overflow

->

java – How to change a TextView’s style at runtime – Stack Overflow

2.然后基本搞定了:

(1)将需要动态设置的样式放到资源里面

在/res/values下面新建xml配置文件:

/res/values/themeConfig.xml

内容为:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <color name="gradientStartColor">#87CEFA</color>
   
    <!-- <color name="GroupSpliterColor">#BEBDDE</color> -->
 <!-- <color name="GroupSpliterColor">#32A6C9</color> -->
    <color name="GroupSpliterColor">#2A2598</color>
    <style name="VariableName">
        <item name="android:textColor">@color/MMBlue1</item>
        <item name="android:textSize">14sp</item>
        <item name="android:textStyle">normal</item>
    </style>
    <style name="VariableValue">
        <item name="android:textSize">14sp</item>
        <item name="android:textStyle">normal</item>
    </style>
   
</resources>

(2)xml中已有TextView的基本定义

/res/layout/variable_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 android:paddingLeft="20dp"
    android:orientation="horizontal" >

        <TextView
            android:id="@+id/variableName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingLeft="5dp"
            android:textColor="@color/MMBlue1"
            android:textSize="14sp" />
        <TextView
            android:id="@+id/variableValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingLeft="5dp"
            android:textSize="14sp" />

</LinearLayout>

(3)代码中就可以动态调用样式资源去配置TextView的样式了

        TextView variableNameView = (TextView) variableLayout.findViewById(R.id.variableName);
        variableNameView.setText(name);
        variableNameView.setTextAppearance(context, R.style.VariableName);
       
        TextView variableValueView = (TextView) variableLayout.findViewById(R.id.variableValue);
        variableValueView.setText(value.toString());
        variableNameView.setTextAppearance(context, R.style.VariableValue);

效果还是不错的:

android runtime dynamic config text style effect

3.后来对于配置文件和代码,都更新升级为:

(1)/res/values/themeConfig.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <color name="gradientStartColor">#87CEFA</color>
   
    <!-- <color name="GroupSpliterColor">#BEBDDE</color> -->
 <!-- <color name="GroupSpliterColor">#32A6C9</color> -->
    <color name="GroupSpliterColor">#2A2598</color>
    <color name="VariableName">#2A2598</color>
    <style name="Variable">
        <item name="android:textSize">14sp</item>
        <item name="android:textStyle">normal</item>
    </style>
    <style name="Variable.Name">
        <item name="android:textColor">@color/VariableName</item>
    </style>
    <style name="Variable.Value">
    </style>
    <style name="Variable.Unit">
    </style>
</resources>

(2)代码中:

        TextView variableNameView = (TextView) variableLayout.findViewById(R.id.variableName);
        variableNameView.setText(name);
        variableNameView.setTextAppearance(context, R.style.Variable_Name);
       
        TextView variableValueView = (TextView) variableLayout.findViewById(R.id.variableValue);
        variableValueView.setText(value.toString());
        variableNameView.setTextAppearance(context, R.style.Variable_Value);
        TextView variableUnitView = (TextView) variableLayout.findViewById(R.id.variableUnit);
        variableUnitView.setText(unit.toString());
        variableUnitView.setTextAppearance(context, R.style.Variable_Unit);

 

【总结】

想要动态在代码中配置字符的样式,主要步骤就是,将样式单独弄成对应的资源resource放在/res/values/xxx.xml中,然后动态在代码中通过setTextAppearance传入对应的R.style.yyy即可。

转载请注明:在路上 » 【已解决】Android中动态设置字符的属性和样式style

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.169 seconds, using 22.11MB memory