Modify the Android App style style method

  • 2020-05-19 05:46:14
  • OfStack

Themes and styles can be customized in android. Style, which is style, we can pull out the attributes of 1, such as length, width, font size, font color, and so on. You can create a new styles.xml file in the res/values directory, which contains the resource root node, and add the item item in the root node. The name of item item is the name of the property, and the value of item item is the value of the property, as shown below:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <style name="MyText" parent="@android:style/TextAppearance">
     <item name="android:textColor">#987456</item>
  <item name="android:textSize">24sp</item>
 </style>
</resources>

style parent a parent class attribute, this property is that the current is inherited from the style style, of course the style contains the attributes of the attribute values, you can also modify the inheritance to the value of the attribute, ok, style finished, we can test 1 effect, write a layout file, for example a TextView what of, you can use this style. I'll just write an EditText here. Here is the layout file:

<?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">
<EditText
 android:id="@+id/myEditText"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 style="@style/MyText"
 android:text=" test 1 Up and down "/>
</LinearLayout>

style, Theme, Theme and style are similar, but Theme is used in Application or Activity, and Style is used in a certain View, there is a difference, ok, no more nonsense, let's look at the code. The following is the style file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <style name="MyText" parent="@android:style/TextAppearance">
  <item name="android:textColor">#987456</item>
  <item name="android:textSize">24sp</item>
 </style>
 <style parent="@android:style/Theme" name="CustomTheme">
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowFrame">@drawable/icon</item>
  <item name="android:windowBackground">?android:windowFrame</item>
</style>
</resources>

style parent a parent class attribute, this property is that the current is inherited from the style style, of course the style contains the attributes of the attribute values, you can also modify the inheritance to the value of the attribute, ok, style finished, we can test 1 effect, write a layout file, for example a TextView what of, you can use this style. I'll just write one EditText here. Here is the layout file:

<?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">
<EditText
 android:id="@+id/myEditText"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 style="@style/MyText"
 android:text=" test 1 Up and down "/>
</LinearLayout>

style, Theme, Theme and style are similar, but Theme is used in Application or Activity, and Style is used in a certain View, there is a difference, ok, no more bullshit, let's look at the code. The following is the style file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <style name="MyText" parent="@android:style/TextAppearance">
  <item name="android:textColor">#987456</item>
  <item name="android:textSize">24sp</item>
 </style>
 <style parent="@android:style/Theme" name="CustomTheme">
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowFrame">@drawable/icon</item>
  <item name="android:windowBackground">?android:windowFrame</item>
</style>
</resources>

As you can see, here is a topic inherited from the default Theme. There are three properties in it. Here is the emphasis on the value of the third property.
Then we add an Theme attribute to Application in Manifest.xml, which corresponds to Theme.

<application android:icon="@drawable/icon" android:label="@string/app_name"
 android:theme="@style/CustomTheme">
<activity android:name=".TestStyle"
 android:label="@string/app_name">
<intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
</activity>

The code above has no title bar, and the background and fram are the images we set. You can also set themes in your code:

package com.test.shang;
import android.app.Activity;
import android.os.Bundle;
public class TestStyle extends Activity {
 @Override
 protected void onCreate (Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setTheme(R.style.CustomTheme);
  setContentView(R.layout.test_style);
 }
}

Related articles: