非常に多くの、そして最終的にこのシリーズで対処したい、最終章、すべてのリソースの統合、統一されたスタイルとして定義されたの前に寝具。
それは何の統一されたスタイルに、この定義は?それのいくつかの例:
- 各ページのタイトルバーのタイトルは、スタイルとして定義することができる等、同じ基本的なフォントサイズ、色、配置、間隔内外の間隔を有するであろう。
- 多くのボタンはまた、背景、間隔、テキストの色、テキストサイズ、テキストの配置やスタイルとして定義することができるような、一貫しています。
- ネットワーク負荷プログレスバーが、実質的にも同じであるが、同じパターンを定義することができます。
- カスタムスタイルがありますポップアップボックスのスタイルが好きではありません。
スタイル定義
Androidのスタイルルート要素<リソース>を有する一般的な定義RES /値/のstyles.xml文書、および各スタイルの特定の定義は、<スタイル>の下のサブラベル<リソース>によって行われる、<スタイル>>項目<複数のを追加することによって、さまざまなスタイル属性を設定します。
さらに、パターンは、<スタイル>親タグの属性の宣言スタイルを継承することができ、継承することができる、(。)継承、親スタイル名プレフィックスの前の点は、サブスタイル名が続くことができます。内蔵のスタイルのAndroidを継承するために、スタイルをカスタマイズするだけで利用できるプレフィックスは、プロパティは、親のみで宣言することができます。
右の場合、Androidの5.0システムのデフォルトボタンのスタイルのための次のコードで説明特定の使用:
<style name="Widget.Material.Button">
<item name="background">@drawable/btn_default_material</item>
<item name="textAppearance">?attr/textAppearanceButton</item>
<item name="minHeight">48dip</item>
<item name="minWidth">88dip</item>
<item name="stateListAnimator">@anim/button_state_list_anim_material</item>
<item name="focusable">true</item>
<item name="clickable">true</item>
<item name="gravity">center_vertical|center_horizontal</item>
</style>
以下の通りであり、アニメーション、button_state_list_anim_material stateListAnimator指定された状態コードが変更されます。
<!-- res/anim/button_state_list_anim_material.xml -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_enabled="true">
<set>
<objectAnimator
android:propertyName="translationZ"
android:duration="@integer/button_pressed_animation_duration"
android:valueTo="@dimen/button_pressed_z_material"
android:valueType="floatType" />
<objectAnimator
android:propertyName="elevation"
android:duration="0"
android:valueTo="@dimen/button_elevation_material"
android:valueType="floatType" />
</set>
</item>
<!-- base state -->
<item android:state_enabled="true">
<set>
<objectAnimator
android:propertyName="translationZ"
android:duration="@integer/button_pressed_animation_duration"
android:valueTo="0"
android:startDelay="@integer/button_pressed_animation_delay"
android:valueType="floatType"/>
<objectAnimator
android:propertyName="elevation"
android:duration="0"
android:valueTo="@dimen/button_elevation_material"
android:valueType="floatType" />
</set>
</item>
<item>
<set>
<objectAnimator
android:propertyName="translationZ"
android:duration="0"
android:valueTo="0"
android:valueType="floatType"/>
<objectAnimator
android:propertyName="elevation"
android:duration="0"
android:valueTo="0"
android:valueType="floatType"/>
</set>
</item>
</selector>
あなたはアニメーションプロパティのアニメーションセット、アニメーション属性の使用方法の各状態は、プロパティアニメーションの記事を参照してください、見ることができます。
次のように今、私は、背景とテキストの色を変更し、Widget.Material.Buttonのスタイルを継承したい、その後、コードは次のとおりです。
<!-- res/values/styles.xml -->
<resources>
<style name="ButtonNormal" parent="@android:style/Widget.Material.Button" >
<item name="android:background">@drawable/bg_btn_selector</item>
<item name="android:textColor">@color/text_btn_selector</item>
</style>
</resources>
これは、描画可能/ bg_btn_selector @および@カラー/ text_btn_selector実装は、参照してください セレクタ論文を。
次のようにいくつかのボタン、私はちょうど、あなたは接頭辞の方法でスタイルのポイント以上のものを継承することができ、テキストの色を変更したい背景が、それを明確にしたい、コードは次のとおりです。
<!-- res/values/styles.xml -->
<resources>
<style name="ButtonNormal" parent="@android:style/Widget.Material.Button">
<item name="android:background">@drawable/bg_btn_selector</item>
<item name="android:textColor">@color/text_btn_selector</item>
</style>
<style name="ButtonNormal.Transparent">
<item name="android:background">@drawable/bg_btn_transparent</item>
<item name="android:textColor">@color/text_btn_selector</item>
</style>
</resources>
その上で対応するボタンを参照するときは、スタイルを追加するたびに、次のように、コードは次のとおりです。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onAction"
android:text="@string/btn_action"
style="@style/ButtonNormal.Transparent" />
時には、あまりにも多くのスタイル定義は、のstyles.xml場合、ファイル上にあり、このファイルには、あまりにも肥大化していること。したがって、パターンは複数のファイルに分割することができます。Androidのシステム自体は、複数のファイルストレージに分割され、すべてのスタイルファイルのリストを次に示します。
styles.xml
styles_device_defaults.xml
styles_holo.xml
styles_leanback.xml
styles_material.xml
styles_micro.xml
themes.xml
themes_device_defaults.xml
themes_holo.xml
themes_leanback.xml
themes_material.xml
themes_micro.xml
その中で、彼らは、2つのカテゴリ、シンプルなスタイルで定義されたスタイルに分かれ、テーマはテーマを定義します。
テーマ
簡単な例は、上記のスタイルだけで最も簡単な使用方法である単一のビューに使用します。しかし、一般的には、テーマ属性、実際にスタイル引用:しかし、スタイルは、また、全体の活動またはアプリケーションのために使用することができ、1つだけ使用状況を表示するために、対応する<活動>タグや<アプリケーション>タグセットアンドロイドにおける必要性は、この時間ではありませんテーマと呼ばれます。
Androidのシステムは、Androidフレームワーク/ベース/コア/ RESの/ RESを見る/ディレクトリ値、話題の複数のセットを提供し、あなたは(今のところ)以下のファイルが表示されます。
- themes.xml:テーマの低いバージョン、ターゲットのAPIレベルは、一般的に10以下であり、
- themes_holo.xml:APIレベル11からテーマを追加
- themes_device_defaults.xml:APIレベル14からテーマを追加
- themes_material.xml:APIレベル21からテーマを追加
- themes_micro.xml:それはAndroid Wearのテーマであるべき
- themes_leanback.xml:それは何不明です
ほとんどは互換機能パックを採用しているのでしかし、実際のアプリケーションでは、通常、テーマの互換性パッケージを採用して提供します:Theme.AppCompatを。APPCOMPATデフォルトのテーマは自動的に、そのようなアンドロイド5.0システムなどのシステムの異なるバージョン、に応じて適切なトピックにマッチします、それは素材のテーマを継承します。しかし、それはまた、問題を引き起こす可能性があり、異なるシステムは、テーマの異なるバージョンを使用して、別の経験があるでしょう。そのため、ユーザーエクスペリエンス、最高のカスタムテーマを統一するためです。
カスタムテーマは、単に親トピックを継承し、また非常に簡単ですし、それに<活動>タグや<アプリケーション>で参照します。
次のように定義されたトピックの例:
<resources>
<style name="AppTheme" parent="Theme.AppCompat">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowAnimationStyle">@style/WindowAnimation</item>
</style>
<!-- Standard animations for a full-screen window or activity. -->
<style name="WindowAnimation" parent="@android:style/Animation.Activity">
<item name="activityOpenEnterAnimation">@anim/activity_open_enter</item>
<item name="activityOpenExitAnimation">@anim/activity_open_exit</item>
<item name="activityCloseEnterAnimation">@anim/activity_close_enter</item>
<item name="activityCloseExitAnimation">@anim/activity_close_exit</item>
</style>
</resources>
前記、WindowAnimation以下はコードactivity_close_exitの一例であり、アクティビティ遷移アニメーションを再割り当て。
<!-- res/anim/activity_close_exit.xml -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:zAdjustment="top">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="@interpolator/decelerate_quart"
android:fillEnabled="true"
android:fillBefore="false"
android:fillAfter="true"
android:duration="200" />
<translate
android:fromYDelta="8%"
android:toYDelta="0"
android:fillEnabled="true"
android:fillBefore="true"
android:fillAfter="true"
android:interpolator="@interpolator/decelerate_quint"
android:duration="350" />
</set>
これは、特定の使用は、アニメーションの記事を表示するのに参照することができ、比較的単純なビューのアニメーション表示アニメーションです。
次に、全体のアプリケーションを使用し、その後のAndroidManifest.xml <アプリケーション*のGT;ラベル設定アンドロイド:テーマプロパティ、次のサンプルコード:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<!-- activity here -->
</application>
転送:キーガン小さな鋼http://keeganlee.me/post/android/20151031