现在的位置: 首页 > 综合 > 正文

Applying themes and styles(3)_Applying themes and styles

2018年04月13日 ⁄ 综合 ⁄ 共 4149字 ⁄ 字号 评论关闭

1.Applying and writing styles(example:)

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyCustomTextView" parent="@android:style/Widget.TextView">
<item name="android:textSize">14sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#CCC</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:padding">5dip</item>
</style>
</resources>

acitivity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:text="Hello, text view!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/MyCustomTextView"
/>

the style attribute is global and not defined under the android XML namespace.

2.Applying and writing themes

The style and theme definition file for the MyMovies application:

values\styles.xml

  <?xml version="1.0" encoding="utf-8"?>
-<resources>
-<style
name
="MyMoviesTheme" parent="@android:style/Theme.Black">
 
<
item name="android:listViewStyle">@style/MyMoviesListView</item>
 
<
item name="android:windowBackground">@drawable/film_bg</item>
  </style>
 <style name="MyMoviesTextAppearance" parent="@android:style/TextAppearance">
  </style>
-<style
name
="MyMoviesListView" parent="@android:Widget.ListView">
 
<
item name="android:background">@color/list_background</item>
 
<
item name="android:listSelector">@drawable/list_selector</item>
 
<
item name="android:cacheColorHint">@android:color/transparent</item>
 
<
item name="android:fastScrollEnabled">true</item>
 
<
item name="android:footerDividersEnabled">false</item>
  </style>
  </resources>

AndroidManifest.xml

<application android:theme="@style/MyMoviesTheme" ...>
...
</application>

3.Styling ListView backgrounds

example:

<resources>
...
<style name="MyMoviesListView" parent="@android:style/Widget.ListView">
<item name="android:background">#A000</item>
<item name="android:cacheColorHint">#0000</item>
...
</style>
</resources>

android:cacheColorHint ell Android which color it should use as the hint color.

if you were to change the default text style to bold red across your entire application, you could do this:
<style name="MyTheme">
<item name="android:textAppearance">@style/MyTextAppearance</item>
</style>
<style name="MyTextAppearance">
<item name="android:textColor">#F00</item>
<item name="android:textStyle">bold</item>
</style>

set your application’s link color to the default text color, you could do this:
<style name="MyTheme">
<item name="android:textColorLink">?android:attr/textColorPrimary</item>
</style>

4.mymovies.project

values\styles.xml

  <?xml version="1.0" encoding="utf-8" ?>
- <resources>
- <style name="MyMoviesTheme" parent="@android:style/Theme.Black">
  <item name="android:listViewStyle">@style/MyMoviesListView</item>
  <item name="android:windowBackground">@drawable/film_bg</item>
  </style>
 <style name="MyMoviesTextAppearance" parent="@android:style/TextAppearance">
  </style>
- <style name="MyMoviesListView" parent="@android:Widget.ListView">
  <item name="android:background">@color/list_background</item>
  <item name="android:listSelector">@drawable/list_selector</item>
  <item name="android:cacheColorHint">@android:color/transparent</item>
  <item name="android:fastScrollEnabled">true</item>
  <item name="android:footerDividersEnabled">false</item>
  </style>
  </resources>

AndroidManifest.xml

<application android:theme="@style/MyMoviesTheme" ...>
...
</application>


values/colors.xml

 <resources>
  <color name="list_background">#A000</color> 
  </resources>

list_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="false" android:drawable="@drawable/list_item_default" /> 
  <item android:state_pressed="true" android:drawable="@drawable/list_item_pressed" /> 
  </selector>

list_selector_default.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <gradient android:startColor="#AFFF" android:endColor="#FFFF" android:angle="0" /> 
  <stroke android:color="#CCC" android:width="1dip" /> 
  <corners android:radius="5px" /> 
  </shape>

list_selector_press.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <gradient android:startColor="#AA66CCFF" android:endColor="#FFFF" android:angle="0" /> 
  <stroke android:color="#CCC" android:width="1dip" /> 
  <corners android:radius="5px" /> 
  </shape>

抱歉!评论已关闭.