Android 单个视图的特定样式
简短问题: 是否可以为各个视图创建主题并设置特定样式 假设我希望TextView的文本颜色为红色,EditText的文本颜色为绿色 这样说(但这不起作用): 我的核心问题是,当我有一个黑色的背景时,一些小部件变得不可见。因此,我需要自定义选择器。不幸的是,您无法以编程方式应用小部件样式。这就是为什么我像约瑟夫在回答中所描述的那样做: attrs.xmlAndroid 单个视图的特定样式,android,android-layout,Android,Android Layout,简短问题: 是否可以为各个视图创建主题并设置特定样式 假设我希望TextView的文本颜色为红色,EditText的文本颜色为绿色 这样说(但这不起作用): 我的核心问题是,当我有一个黑色的背景时,一些小部件变得不可见。因此,我需要自定义选择器。不幸的是,您无法以编程方式应用小部件样式。这就是为什么我像约瑟夫在回答中所描述的那样做: attrs.xml <resources> <attr name="myEditTextStyle" format="reference" /&
<resources>
<attr name="myEditTextStyle" format="reference" />
<attr name="myCheckBoxStyle" format="reference" />
@可绘制/我的时间\u编辑\u文本\u全息\u暗
#ffffff
@可提取/我的时间\u btn\u检查\u全息\u暗
及
干杯,
Stefan如果您希望完全控制单个元素(即,一个页面上的两个文本视图可能不同,或者不同页面上的文本视图可能不同),但希望它可以由主题控制:
res/values/styles.xml
<resources>
<style name="MyAppTheme" parent="android:Theme.Light">
<item name="android:windowNoTitle">true</item>
<item name="myTextAppearance">@style/MyTextAppearance</item>
<item name="myEditTextStyle">@style/MyEditTextStyle</item>
</style>
<style name="MyTextAppearance" parent="android:TextAppearance">
<item name="android:textColor">#0000ff</item>
</style>
<style name="MyEditTextStyle" parent="android:Widget.EditText">
<item name="android:textColor">#ff0000</item>
</style>
</resources>
<resources>
<attr name="myTextAppearance" format="reference" />
<attr name="myEditTextStyle" format="reference" />
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textAppearance="?attr/myTextAppearance"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message Here"
style="?attr/myEditTextStyle"
/>
</LinearLayout>
<resources>
<style name="MyAppTheme" parent="android:Theme.Light">
<item name="android:windowNoTitle">true</item>
<item name="android:textColorPrimary">#0000ff</item>
<item name="android:editTextStyle">@style/MyEditTextStyle</item>
</style>
<style name="MyEditTextStyle" parent="android:Widget.EditText">
<item name="android:textColor">#ff0000</item>
</style>
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message Here"
/>
</LinearLayout>
真的
@样式/我的文本外观
@样式/MyEditTextStyle
#0000ff
#ff0000
res/values/attrs.xml
<resources>
<style name="MyAppTheme" parent="android:Theme.Light">
<item name="android:windowNoTitle">true</item>
<item name="myTextAppearance">@style/MyTextAppearance</item>
<item name="myEditTextStyle">@style/MyEditTextStyle</item>
</style>
<style name="MyTextAppearance" parent="android:TextAppearance">
<item name="android:textColor">#0000ff</item>
</style>
<style name="MyEditTextStyle" parent="android:Widget.EditText">
<item name="android:textColor">#ff0000</item>
</style>
</resources>
<resources>
<attr name="myTextAppearance" format="reference" />
<attr name="myEditTextStyle" format="reference" />
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textAppearance="?attr/myTextAppearance"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message Here"
style="?attr/myEditTextStyle"
/>
</LinearLayout>
<resources>
<style name="MyAppTheme" parent="android:Theme.Light">
<item name="android:windowNoTitle">true</item>
<item name="android:textColorPrimary">#0000ff</item>
<item name="android:editTextStyle">@style/MyEditTextStyle</item>
</style>
<style name="MyEditTextStyle" parent="android:Widget.EditText">
<item name="android:textColor">#ff0000</item>
</style>
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message Here"
/>
</LinearLayout>
res/layout/layout.xml
<resources>
<style name="MyAppTheme" parent="android:Theme.Light">
<item name="android:windowNoTitle">true</item>
<item name="myTextAppearance">@style/MyTextAppearance</item>
<item name="myEditTextStyle">@style/MyEditTextStyle</item>
</style>
<style name="MyTextAppearance" parent="android:TextAppearance">
<item name="android:textColor">#0000ff</item>
</style>
<style name="MyEditTextStyle" parent="android:Widget.EditText">
<item name="android:textColor">#ff0000</item>
</style>
</resources>
<resources>
<attr name="myTextAppearance" format="reference" />
<attr name="myEditTextStyle" format="reference" />
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textAppearance="?attr/myTextAppearance"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message Here"
style="?attr/myEditTextStyle"
/>
</LinearLayout>
<resources>
<style name="MyAppTheme" parent="android:Theme.Light">
<item name="android:windowNoTitle">true</item>
<item name="android:textColorPrimary">#0000ff</item>
<item name="android:editTextStyle">@style/MyEditTextStyle</item>
</style>
<style name="MyEditTextStyle" parent="android:Widget.EditText">
<item name="android:textColor">#ff0000</item>
</style>
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message Here"
/>
</LinearLayout>
如果不希望控制单个视图,则可以覆盖默认值:
res/values/styles.xml
<resources>
<style name="MyAppTheme" parent="android:Theme.Light">
<item name="android:windowNoTitle">true</item>
<item name="myTextAppearance">@style/MyTextAppearance</item>
<item name="myEditTextStyle">@style/MyEditTextStyle</item>
</style>
<style name="MyTextAppearance" parent="android:TextAppearance">
<item name="android:textColor">#0000ff</item>
</style>
<style name="MyEditTextStyle" parent="android:Widget.EditText">
<item name="android:textColor">#ff0000</item>
</style>
</resources>
<resources>
<attr name="myTextAppearance" format="reference" />
<attr name="myEditTextStyle" format="reference" />
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textAppearance="?attr/myTextAppearance"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message Here"
style="?attr/myEditTextStyle"
/>
</LinearLayout>
<resources>
<style name="MyAppTheme" parent="android:Theme.Light">
<item name="android:windowNoTitle">true</item>
<item name="android:textColorPrimary">#0000ff</item>
<item name="android:editTextStyle">@style/MyEditTextStyle</item>
</style>
<style name="MyEditTextStyle" parent="android:Widget.EditText">
<item name="android:textColor">#ff0000</item>
</style>
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message Here"
/>
</LinearLayout>
真的
#0000ff
@样式/MyEditTextStyle
#ff0000
res/layout/layout.xml
<resources>
<style name="MyAppTheme" parent="android:Theme.Light">
<item name="android:windowNoTitle">true</item>
<item name="myTextAppearance">@style/MyTextAppearance</item>
<item name="myEditTextStyle">@style/MyEditTextStyle</item>
</style>
<style name="MyTextAppearance" parent="android:TextAppearance">
<item name="android:textColor">#0000ff</item>
</style>
<style name="MyEditTextStyle" parent="android:Widget.EditText">
<item name="android:textColor">#ff0000</item>
</style>
</resources>
<resources>
<attr name="myTextAppearance" format="reference" />
<attr name="myEditTextStyle" format="reference" />
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textAppearance="?attr/myTextAppearance"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message Here"
style="?attr/myEditTextStyle"
/>
</LinearLayout>
<resources>
<style name="MyAppTheme" parent="android:Theme.Light">
<item name="android:windowNoTitle">true</item>
<item name="android:textColorPrimary">#0000ff</item>
<item name="android:editTextStyle">@style/MyEditTextStyle</item>
</style>
<style name="MyEditTextStyle" parent="android:Widget.EditText">
<item name="android:textColor">#ff0000</item>
</style>
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message Here"
/>
</LinearLayout>
实际上我想避免这种情况,因为我不想将样式添加到每个单独的视图中。我想在一个主题中设置一次。另外,用户应该可以在设置中更改主题。我已经为自己绘制的视图定制了主题。我通过实现一个抽象主题类和几个返回特定于主题的颜色和绘图的实现来实现这一点。。。您上面发布的方法无法满足我的需求,因为这需要每个主题都有一个布局文件。事实上,我只是想根据用户设置调用“Activity.setTheme(R.drawable.RedTheme)”,更新我的答案并使其成为主题,谢谢!要阅读一些文档以了解您在上面发布的内容^^^好的,明白了!这意味着我的问题的答案是:不,不能在自定义主题中为特定的小部件类型设置小部件样式(就像在CSS样式表中一样)。您必须创建别名并将其应用于每个小部件。你同意吗?