mmo*_*mmo 18 android colors android-linearlayout
为了给我的应用程序的用户指示哪个字段当前具有焦点我正在尝试根据当前状态更改某些字段的背景颜色,但是,我在理解Androids颜色状态列表资源时遇到了麻烦:
我找到了示例(对不起,URL不再有效),如果我尝试完全相同,即如果我想调整textColor,事情确实有效.但是,如果我尝试的只是略有不同的东西,即相适应的背景颜色,事情不工作,我不明白为什么?为什么这么不一致?
为了更容易理解我想要做什么,我附加了我的混合物..xml文件:
该AndroidManifest.xml
文件中:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mmo.android.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Test"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
的Test-Activity
:
package mmo.android.test;
import android.app.Activity;
import android.os.Bundle;
public class Test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Run Code Online (Sandbox Code Playgroud)
的res/values/strings.xml
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Test!</string>
<string name="app_name">Test</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
res/color/button_test_color.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#f0f"/> <!-- pressed -->
<item android:state_focused="true" android:color="#ff0"/> <!-- focused -->
<item android:color="#000"/> <!-- default -->
</selector>
Run Code Online (Sandbox Code Playgroud)
最后我的res/layout/main.xml
档案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="foobar"
android:textColor="@color/button_test_color"
android:background="#f00"
/>
<!--
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="foobar"
android:textColor="#f00"
android:background="@color/button_test_color"
/>
-->
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
如果我按照此处所示运行它,它就可以工作,即我得到一个按钮,其文本颜色会根据按钮是否聚焦,按下等而改变.
如果我取消注释下面的按钮,我只是翻转textColor和background的属性值我得到一个例外,说明
... <item> tag requires a 'drawable' attribute or child tag defining a drawable
Run Code Online (Sandbox Code Playgroud)
我到底错过了什么?为什么该颜色状态列表可以作为文本颜色而不是作为背景颜色接受?如何根据视图的状态指定视图的背景颜色?
Doc*_*s23 58
我有这个确切的问题.在我看来,android:background
它不适用于颜色状态列表.我通过创建一个State List Drawable来解决这个问题(单个颜色可以用作State List中的drawable).
要使用您的示例,请创建一个文件res/drawable/button_test_background.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/pressed_color"/>
<item android:state_focused="true" android:drawable="@color/focused_color"/>
<item android:drawable="@color/default_color"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
注意使用android:drawable
而不是android:color
.Android将使用颜色资源并使其成为可绘制的.要完成此操作,您需要将颜色资源添加到您的res/values/colors.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<color name="pressed_color">#f0f</color>
<color name="focused_color">#ff0</color>
<color name="default_color">#000</color>
...
</resources>
Run Code Online (Sandbox Code Playgroud)
然后,您将使用@drawable/button_test_background
而不是使用此drawable引用@color/button_test_color
.
因此,总之,颜色状态列表可以正常工作android:textColor
,但是对于android:background
上面的State List Drawable方法是必需的.
Aar*_*n C -1
尝试将 textColor 定义为可绘制对象,而不是颜色:
android:textColor="@drawable/button_test_color"
Run Code Online (Sandbox Code Playgroud)
资源类别基于它们是什么类型的资源,而不是它们所在的文件夹的名称。button_test_color.xml 形式的 XML 文件通常被引用为“drawable” - 我实际上很惊讶“color”竟然能起作用!
归档时间: |
|
查看次数: |
21351 次 |
最近记录: |