android在运行时动态改变样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3241729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:17:03  来源:igfitidea点击:

android dynamically change style at runtime

androidcoding-style

提问by Ben

I'd like to make my font sizes configurable but i'd also like to make use of the style tag in my layouts. Is it possible to change the definition of a style at runtime? or is the only option to manually change the individual style elements on each textview etc.?

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

采纳答案by AhmadAssaf

Changing the style after creating the view is not supported .. so what you can do is:

不支持在创建视图后更改样式.. 所以你可以做的是:

  1. create a new android xml file of type values
  2. add new theme
  3. add your elements to that theme and their values and save the file
  1. 创建一个新的 android xml 类型值文件
  2. 添加新主题
  3. 将您的元素添加到该主题及其值并保存文件

Now, when you are dynamically creating the new view you call the constructor that will allow to define a defStyle. Then, you point to the style ID you have just created by pointing to R."the XML file name"."your style ID"

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

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

回答by Janarthanan

Following sample code changes the size/style of the text dynamically on runtime.

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

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

活动.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);
        }
    } );
}

回答by EboMike

I'm not sure if this will work in your case, but you can create themes that define your styles. There's Activity.setTheme()which you pass in a theme XML file. The theme contains a bunch of definitions.

我不确定这是否适用于您的情况,但您可以创建定义样式的主题。Activity.setTheme()您在主题 XML 文件中传递了其中的内容。主题包含一堆定义。

I've only used it to override certain global styles, like background color, I don't know if you can use it to define styles that your widgets will use. It's worth giving it a shot though. If it works, please let me know!

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