假设我希望TextView我的应用中的所有实例都有textColor="#ffffff".有没有办法在一个地方设置它而不是为每个地方设置它TextView?
我进入了attrs.xml
<resources>
<!-- theme specific colors -->
<attr format="reference|color" name="foreground" />
<attr format="reference|color" name="background" />
</resources>
Run Code Online (Sandbox Code Playgroud)
然后在theme.xml中
<style name="MyTheme" parent="android:Theme.Black">
<item name="android:windowNoTitle">true</item>
<item name="foreground">#0000FF</item>
<item name="background">#00FF00</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我还创建了名为forground_to_background.xml的颜色选择器
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="?background"/> <!-- pressed -->
<item android:state_focused="true" android:color="?background"/> <!-- focused -->
<item android:color="?foreground"/> <!-- default -->
</selector>
Run Code Online (Sandbox Code Playgroud)
现在我想在TextView中一起使用它:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/forground_to_background" />
Run Code Online (Sandbox Code Playgroud)
不幸的是它不起作用.我没有漂亮的绿蓝色,只有一种颜色 - 红色.TextView始终为红色.当我将TextView更改为使用"?foreground"时,颜色会发生变化.此外,当我将颜色选择器从"?xxxx"更改为硬编码值时,"#00f"颜色开始起作用.
哪里有问题?我究竟做错了什么?
编辑: 我认为它是问题/错误的重复选择器资源可以使用样式中定义的颜色吗?
Edit2: 此外,当我尝试在ListView应用程序中使用此TextView时崩溃.它无法解析XML.
我有一套类似的应用程序.我将公共资源和代码提取到库项目,应用程序只是覆盖了他们需要的东西.在我的库中,定义了以下样式:
<style name="ListItemText">
<item name="android:layout_toRightOf">@id/preview_image</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">@dimen/previewTextSize</item>
</style>
Run Code Online (Sandbox Code Playgroud)
如您所见,它包含android:layout_toRightOf属性,因为此样式将应用于ListView行中的文本,该行应显示在该行中图像的右侧.
但是在我的一个应用程序中,我想在图像下方显示文本.ListItemText应该如何定义该应用程序中的样式以覆盖android:layout_toRightOf属性值并将其替换为android:layout_below?
如果我将其定义为:
<style name="ListItemText">
<item name="android:layout_below">@id/preview_image</item>
</style>
Run Code Online (Sandbox Code Playgroud)
它在图像的右侧和下方显示文本,有效地总结了库和应用程序样式XML的属性.
ps:我的问题的一个可能的解决方案是摆脱android:layout_toRightOf样式并将其移动到布局xml.然后在目标应用程序中,可以重新定义/覆盖此布局.但我正在寻找基于样式的解决方案,因为它可以提供更简单直接的属性覆盖方式.
(我也可以使用带有parent属性的继承样式,但这将再次要求应用程序中的布局覆盖,我试图避免).
所以这是我的问题:我创建了一个扩展的自定义组件.Button这个按钮有一个名为的属性testAttr.
<declare-styleable name="TestButton" parent="@android:style/Widget.Button">
<attr name="testAttr" format="reference|color"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
我想为这个组件创建一个默认样式,所以我添加了这个:
<declare-styleable name="TestTheme">
<attr name="testStyle" format="reference"/>
</declare-styleable>
<style name="Theme.AppTheme" parent="@android:style/Theme" >
<item name="testStyle">@style/test</item>
</style>
Run Code Online (Sandbox Code Playgroud)
问题是我想要同时使用android属性和我自己的属性,这是行不通的:
<style name="test" parent="@android:style/Widget.Button">
<item name="android:background">#FF0000</item>
<item name="testAttr">testText</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我在自定义组件中设置默认样式,如下所示:
public myButton(final Context context) {
this(context, null);
}
public myButton(final Context context, final AttributeSet attrs) {
this(context, attrs, R.styleable.TestTheme_testStyle);
}
public myButton(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
final TypedArray array = context.obtainStyledAttributes(
attrs,
R.styleable.TestButton,
defStyle,
R.style.testDefault);
final …Run Code Online (Sandbox Code Playgroud) 带有标签的我的应用有两个主题.在每个主题中,选项卡具有处于选定和未选定状态的不同图像.我如何按主题正确引用图像?
例如.我有themes.xml
<?xml version="1.0" encoding="utf-8"?>
<style name="LightTheme" parent="@android:style/Theme.Light">
<item name="tabShows">@drawable/ic_tab_shows_unselected_light</item>
<item name="tabShowsSelected">@drawable/ic_tab_shows_selected_light</item>
<item name="tabNews">@drawable/ic_tab_news_selected_light</item>
<item name="tabNewsSelected">@drawable/ic_tab_news_unselected_light</item>
</style>
<style name="DarkTheme" parent="@android:style/Theme.Black">
<item name="tabShows">@drawable/ic_tab_shows_unselected_dark</item>
<item name="tabShowsSelected">@drawable/ic_tab_shows_selected_dark</item>
<item name="tabNews">@drawable/ic_tab_news_selected_dark</item>
<item name="tabNewsSelected">@drawable/ic_tab_news_unselected_dark</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我还有一个tab_shows.xml和tab_news.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/ic_tab_shows_selected_light"/>
<item android:state_selected="false" android:drawable="@drawable/ic_tab_shows_unselected_light" />
Run Code Online (Sandbox Code Playgroud)
如何根据当前主题在选择器中引用所需的图像?这不适合我
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="?tabShowsSelected"/>
<item android:state_selected="false" android:drawable="?tabShows" />
Run Code Online (Sandbox Code Playgroud)
在布局文件中这是有效的,我的意思是通过?styleName引用样式
如何通过样式设置LinearLayout的TextView子项的样式.像这样的东西:
<style name="MyLinearayoutStyle" parent="MyParentStyle">
<item name="android:background">@drawable/mybackground</item>
<item name="android:padding>5dp</item>
// Here i need to define my textview style
</style>
<style name="MyTextViewStyle> parent="AnotherParentStyle">
<item name="android:textColor">@color/Black</item>
<item name="android:textSize">15sp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我尝试了android:TextAppearance和android:textStyle,但这没有用.
我想创建一个按钮背景,它与 ?attr/selectableItemBackground 完全相同,但具有黑色描边轮廓。
有没有办法“扩展” ?attr/selectableItemBackground 并添加黑色轮廓,或者我是否必须创建一个全新的 stateList?
例如,当我在XML中定义样式标记时,所有类型的所有视图都会获得该主题.视图类型的样式之间是否有任何解决方案?这是一些代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="buttonTheme" parent="android:Theme.Holo">
<item name="android:drawableLeft">@drawable/ic_launcher</item>
</style>
<style name="textTheme" parent="android:Theme.Holo">
<item name="android:textColor">#ff0000</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)