簡體   English   中英

Android 風格和主題的區別

[英]Android styles and themes difference

根據文檔:主題是應用於整個 Activity 或應用程序的樣式,而不是單個 View

但是當我在 xml 中將主題添加到我的視圖(文本視圖)時,它被編譯並工作。 我認為當我想添加到單個視圖時,我需要使用“樣式”。

<TextView
android:id="@+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/TestTheme"
android:textSize="25dp"
android:text="TextView" />

有人可以向我解釋嗎?

樣式是指定視圖外觀和格式的屬性集合。 樣式可以指定諸如高度、填充、字體顏色、字體大小、背景顏色等屬性。

例如,通過使用樣式,您可以采用此布局 XML:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#00FF00"
    android:typeface="monospace"
    android:text="@string/hello" />

並將其變成這樣:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="@style/CodeFont"
    android:text="@string/hello" />

與樣式相關的屬性已從布局 XML 中刪除,並放入名為CodeFont的樣式定義中,然后使用 android:textAppearance 屬性應用該定義。

要創建CodeFont樣式,請在項目的 res/values/ 目錄中保存一個 XML 文件。 下面是一個例子:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
    </style>
</resources>

這個示例樣式可以從 XML 布局引用為 @style/CodeFont,如上面的 TextView 所示。 一個主題就是一種風格 唯一的區別是主題是應用於整個 Activity 或應用程序的樣式,而不是單個視圖。

當樣式作為主題應用時,活動或應用程序中的每個視圖都會應用它支持的每個樣式屬性。 例如,如果您應用相同的 CodeFont 樣式作為活動的主題,則該活動中的所有文本都以綠色等寬字體顯示。

<resources>
    <style name="AppTheme" parent="Theme.Material">
        <item name="colorPrimary">#673AB7</item>
        <item name="colorPrimaryDark">#512DA8</item>
        <item name="colorAccent">#FF4081</item>
    </style>
</resources>

以下示例說明使用引用為同一屬性指定值:

<resources>
    <style name="AppTheme" parent="Theme.Material">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/accent</item>
    </style>
</resources>

要為應用程序的所有活動設置主題,請打開 AndroidManifest.xml 文件並編輯標記以包含 android:theme 屬性和樣式名稱。 例如:

<application android:theme="@style/CustomTheme">

如果您希望將主題僅應用於應用程序中的一個活動,請改為將 android:theme 屬性添加到標簽中。

正如 Android 提供其他內置資源一樣,您可以使用許多預定義的主題,以避免自己編寫它們。 例如,您可以使用 Dialog 主題並使您的活動看起來像一個對話框:

<activity android:theme="@android:style/Theme.Dialog">

或者,如果您希望背景透明,請使用半透明主題:

<activity android:theme="@android:style/Theme.Translucent">

您需要創建小部件樣式並將其添加到您的應用主題中,最后在清單中添加您的應用主題。 無需在每個視圖中提及樣式。 像這樣在樣式文件中的內部值文件夾:

   <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textViewStyle">@style/TextViewStyle</item>
   </style>

   <style name="TextViewStyle" parent="android:Widget.TextView">
    <item name="android:background">@color/TextView_background</item>
    <item name="android:textColor">#16F20A</item>
    <item name="android:textStyle">bold</item>
   </style>


In Your manifest file  inside application tag add the below line.
    <application
      android:theme="@style/AppTheme">
    </application>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM