スタイル リソース
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
スタイル リソースは、UI の形式と外観を定義します。スタイルは、個々の View
に適用することも(この場合はレイアウト ファイル内から)、Activity
全体やアプリ全体に適用することもできます(この場合はマニフェスト ファイル内から)。
スタイルの作成方法と適用方法については、スタイルとテーマをご覧ください。
注: スタイルは、name
属性で提供される値を使用して参照されるシンプルなリソースです(XML ファイルの名前を使用して参照されるわけではありません)。そのため、単一の XML ファイル内で、単一の <resources>
要素の下で、スタイル リソースを他のシンプルなリソースと組み合わせることができます。
- ファイルの場所:
res/values/filename.xml
ファイル名は任意です。要素の name
がリソース ID として使用されます。
- リソースの参照:
-
XML 内:
@[package:]style/style_name
- 構文:
-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style
name="style_name"
parent="@[package:]style/style_to_inherit">
<item
name="[package:]style_property_name"
>style_value</item>
</style>
</resources>
- 要素:
-
<resources>
- 必須。ルートノードにする必要があります。
属性はありません。
<style>
- 1 つのスタイルを定義します。
<item>
要素を格納します。
属性:
name
- 文字列。必須。スタイルの名前。ビュー、アクティビティ、アプリにスタイルを適用する際のリソース ID として使用されます。
parent
- スタイル リソース。このスタイルがスタイル プロパティを継承する継承元スタイルへの参照。
<item>
- スタイルのプロパティを 1 つ定義します。この要素は、
<style>
要素の子要素にする必要があります。
属性:
name
- 属性リソース。必須。定義するスタイル プロパティの名前。必要に応じてパッケージ プレフィックスを付けます(たとえば、
android:textColor
)。
- 例:
-
- 次のスタイル用 XML ファイルを
res/values/
に保存するとします。
-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomText" parent="@style/Text">
<item name="android:textSize">20sp</item>
<item name="android:textColor">#008</item>
</style>
</resources>
- 次の XML ファイルは、上記のスタイルを
TextView
に適用します(res/layout/
に保存)。
-
<?xml version="1.0" encoding="utf-8"?>
<EditText
style="@style/CustomText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-07-27 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-27 UTC。"],[],[],null,["# Style resource\n\nA style resource defines the format and look for a UI.\nA style can be applied to an individual [View](/reference/android/view/View) (from within a layout file) or to\nan entire [Activity](/reference/android/app/Activity) or application (from within the manifest file).\n\nFor more information about creating and applying styles, please read\n[Styles and Themes](/guide/topics/ui/themes).\n\n**Note:** A style is a simple resource that is referenced\nusing the value provided in the `name` attribute (not the name of the XML file). As\nsuch, you can combine style resources with other simple resources in the one XML file,\nunder one `\u003cresources\u003e` element.\n\nfile location:\n: `res/values/`*filename*`.xml` \n\n The filename is arbitrary. The element's `name` will be used as the resource ID.\n\nresource reference:\n:\n In XML: `@[package:]style/`*style_name*\n\nsyntax:\n:\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cresources\u003e\n \u003cstyle\n name=\"style_name\"\n parent=\"@[package:]style/style_to_inherit\"\u003e\n \u003citem\n name=\"[package:]style_property_name\"\n \u003estyle_value\u003c/item\u003e\n \u003c/style\u003e\n \u003c/resources\u003e\n ```\n\nelements:\n:\n\n `\u003cresources\u003e`\n : **Required.** This must be the root node.\n\n No attributes.\n\n `\u003cstyle\u003e`\n : Defines a single style. Contains `\u003citem\u003e` elements.\n\n attributes:\n\n `name`\n : *String* . **Required**. A name for the style, which is used as the\n resource ID to apply the style to a View, Activity, or application.\n\n `parent`\n : *Style resource*. Reference to a style from which this\n style should inherit style properties.\n\n\n `\u003citem\u003e`\n : Defines a single property for the style. Must be a child of a\n `\u003cstyle\u003e` element.\n\n \u003cbr /\u003e\n\n\n attributes:\n\n `name`\n : *Attribute resource* . **Required** . The name of the style property\n to be defined, with a package prefix if necessary (for example `android:textColor`).\n\n\nexample:\n:\n\n XML file for the style (saved in `res/values/`):\n :\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cresources\u003e\n \u003cstyle name=\"CustomText\" parent=\"@style/Text\"\u003e\n \u003citem name=\"android:textSize\"\u003e20sp\u003c/item\u003e\n \u003citem name=\"android:textColor\"\u003e#008\u003c/item\u003e\n \u003c/style\u003e\n \u003c/resources\u003e\n ```\n\n XML file that applies the style to a [TextView](/reference/android/widget/TextView)\n (saved in `res/layout/`):\n :\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cEditText\n style=\"@style/CustomText\"\n android:layout_width=\"fill_parent\"\n android:layout_height=\"wrap_content\"\n android:text=\"Hello, World!\" /\u003e\n ```"]]