android dynamicznie zmienia styl w czasie wykonywania

Chciałbym, aby moje rozmiary czcionek były konfigurowalne, ale chciałbym również użyć znacznika stylu w moich układach. Czy można zmienić definicję stylu w czasie wykonywania? lub jest jedyną opcją ręcznego zmieniania poszczególnych elementów stylu w każdym widoku tekstowym itd.?

Author: Ben, 2010-07-14

3 answers

Zmiana stylu po utworzeniu widoku nie jest obsługiwana .. więc to co możesz zrobić to:

  1. Utwórz nowy plik XML Androida o wartościach typu
  2. Dodaj nowy temat
  3. Dodaj swoje elementy do tego motywu i ich wartości i zapisz plik

Teraz, gdy dynamicznie tworzysz nowy widok, wywołujesz konstruktor, który pozwoli zdefiniować defStyle. Następnie wskaż identyfikator stylu, który właśnie stworzyłeś, wskazując na R. "nazwa pliku XML"."Twój styl ID "

myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);
 12
Author: AhmadAssaf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-04 14:18:45

Następujący przykładowy kod zmienia rozmiar / styl tekstu dynamicznie w trybie 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>

Aktywność.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);
        }
    } );
}
 87
Author: Janarthanan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-02-08 10:24:13

Nie jestem pewien, czy to zadziała w Twoim przypadku, ale możesz tworzyć motywy, które definiują Twoje style. Jest Activity.setTheme(), który przekazujesz w pliku XML motywu. Temat zawiera kilka definicji.

Używałem go tylko do nadpisywania niektórych stylów globalnych, takich jak kolor tła, Nie wiem, czy możesz go użyć do definiowania stylów, których będą używać Twoje widżety. Warto jednak spróbować. Jeśli to zadziała, daj mi znać!

 4
Author: EboMike,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-13 22:12:09