Android动态更改运行时样式

43

我想让我的字体大小可配置,但我也想在我的布局中使用样式标签。是否可以在运行时更改样式的定义?还是唯一的选项是手动更改每个文本视图等上的各个样式元素?

我希望能够使字体大小可配置,同时在布局中使用样式标签。请问是否可以在运行时更改样式的定义?或者唯一的选项是手动更改每个文本视图等上的各个样式元素?请注意保留HTML标记。

https://dev59.com/0m445IYBdhLWcg3wZJiw#12591991 - mixel
3个回答

101

以下示例代码可以在运行时动态更改文本的大小/样式。

attrs.xml

  <?xml version="1.0" encoding="utf-8"?>
  <resources>
       <!-- View styles -->
       <attr name="textTitle" format="reference" />
       <attr name="textBody" format="reference" />
  </resources>

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="small_title_text">
      <item name="android:textSize">22sp</item>
      <item name="android:textColor">@color/green</item>
      <item name="android:textStyle">normal</item>
      <item name="android:paddingBottom">5dip</item>
   </style>
   <style name="small_body_text">
      <item name="android:textSize">16sp</item>
      <item name="android:textColor">@color/white</item>
      <item name="android:textStyle">normal</item>
      <item name="android:paddingBottom">5dip</item>
   </style>
   <style name="large_title_text">
      <item name="android:textSize">28sp</item>
      <item name="android:textColor">@color/red</item>
      <item name="android:textStyle">normal</item>
      <item name="android:paddingBottom">5dip</item>
   </style>

   <style name="large_body_text">
      <item name="android:textSize">20sp</item>
      <item name="android:textColor">@color/white</item>
      <item name="android:textStyle">normal</item>
      <item name="android:paddingBottom">5dip</item>
   </style>

  <!-- Base application theme is the default theme. -->
  <style name="Theme" parent="android:Theme">
  </style>

  <style name="Theme.Small">
     <item name="textTitle">@style/small_title_text</item>
     <item name="textBody">@style/small_body_text</item>
  </style>

  <style name="Theme.Large">
      <item name="textTitle">@style/large_title_text</item>
      <item name="textBody">@style/large_body_text</item>
  </style>
 </resources>

main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     >

 <RadioGroup
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
<RadioButton 
    android:text="Large Text" 
    android:id="@+id/textSizeLarge" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
</RadioButton>
<RadioButton 
    android:text="Small Text" 
    android:id="@+id/textSizeSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
</RadioButton>
 </RadioGroup>
 <TextView  
      android:id="@+id/title" 
style="?textTitle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Select the size of the text"
     />
 <TextView  
    android:id="@+id/body" 
    style="?textBody" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/message"
     />
 </LinearLayout>

Activity.java

     public void onCreate(Bundle savedInstanceState) {
         if ("Large".equalsIgnoreCase( getIntent().getStringExtra( "Theme" )))
         {
             setTheme(R.style.Theme_Large);
         }
         else if ("Small".equalsIgnoreCase( getIntent().getStringExtra( "Theme" )))
         {
             setTheme(R.style.Theme_Small);
         }
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         RadioButton largeText = ( RadioButton ) findViewById( R.id.textSizeLarge );
         largeText.setOnClickListener( new OnClickListener() {
             public void onClick( View view ) {
                 Toast.makeText(context, "Large Text Selected", Toast.LENGTH_SHORT).show();
            Intent intent = getIntent();
            intent.putExtra( "Theme", "Large" );
            finish();
            startActivity(intent);
        }
    } );
    
    RadioButton smallText = ( RadioButton ) findViewById( R.id.textSizeSmall );
    smallText.setOnClickListener( new OnClickListener() {
        public void onClick( View view ) {
            Toast.makeText(context, "Small Text Selected", Toast.LENGTH_SHORT).show();
            Intent intent = getIntent();
            intent.putExtra( "Theme", "Small" );
            finish();
            startActivity(intent);
        }
    } );
}

我如何在运行时更改按钮的主题(在创建后)? - blow

19

创建视图后更改样式不受支持..所以你可以这样做:

  1. 创建一个新的Android xml文件类型的值
  2. 添加新的主题
  3. 将您的元素及其值添加到该主题中并保存文件

现在,当您动态创建新视图时,调用将允许定义defStyle的构造函数。然后,通过指向R."XML文件名"."您的样式ID"来指向刚创建的样式ID。

myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);

1
这里,我认为您为特定的文本视图设置了文本外观,如果我需要对Android TextView类执行相同操作,以便它将应用于所有文本视图,那么该如何实现? - Ajay Sharma

6

我不确定这种方法是否适用于你的情况,但你可以创建定义样式的主题。有一个Activity.setTheme()方法,你需要传入一个主题XML文件。主题包含许多定义。

我只用它来覆盖某些全局样式,比如背景颜色,我不知道是否可以使用它来定义小部件将使用的样式。不过试一试也无妨。如果有效,请告诉我!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接