在按下时查看:按下时更改背景颜色?如何显示正在按下视图?
Posted
技术标签:
【中文标题】在按下时查看:按下时更改背景颜色?如何显示正在按下视图?【英文标题】:View on press onpress: Change background color on press? How do I show that the View is being pressed? 【发布时间】:2011-01-01 20:12:21 【问题描述】:我目前有一个自定义视图,以 9-patch 图像作为边框。
该自定义视图在 LinearLayout 中放置了 3 次,所以它看起来像这样:
+------------------------+
| CustomView |
+------------------------+
| CustomView |
+------------------------+
| CustomView |
+------------------------+
我已经为视图附加了一个点击事件监听器,所以它是可点击的。但后来我点击它,我看不到我在点击它——颜色没有变化。
所以,我想我会附加一个“onPress”侦听器,然后更改视图的背景,但我找不到这样的侦听器。
那么,如何在视图上创建行为,以便我可以看到它被按下了?这通常在 android 中完成,绿色背景表示它正在被按下。
【问题讨论】:
【参考方案1】:您可以为视图设置 OnClickListener。单击视图时将调用它。但是对于像单击视图时更改背景这样简单的事情,您应该使用有状态的可绘制对象。它们是这样工作的,你制作了 3 个 9-patch 图像。
-
是像现在这样的普通背景。
是当用户使用轨迹球/方向键选择视图时背景应该是什么样子
是用户点击视图时的样子
然后你在你的 drawable 文件夹中创建一个新的 xml 文件。它应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/background_pressed" />
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/background_focused" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/background_pressed" />
<item
android:drawable="@drawable/background_normal" />
然后,当您设置视图的背景时,将其设置为 xml 文件。
【讨论】:
Thx for that =) 所以,我有点接近解决方案。而不是试图找到一个“onPress”的东西,我只是创建了 3 个不同的图像并将它们放在 Selector-XML-file 中,然后将 Selector-XML-file 附加为背景?我会试试的 =) thx! 嗯,在 [ android:drawable="@drawable/background_pressed" ] 我找不到我的可绘制对象。如果我尝试用我的图像替换 background_pressed,它不会出现。我错过了什么吗? 嗯,它似乎没有工作......它就像正常 - 没有改变背景...... 我猜这与我在指定 android:drawable="..." 时没有得到建议的事实有关 您是在 Eclipse 中添加 XML 还是只是添加到文件系统中?如果您将它添加到文件系统中,则需要刷新 eclipse 文件夹。至于找不到drawable,你需要确保你在xml中输入的名称与图像的名称完全相同,减去扩展名。并且您需要确保将视图设置为可点击和可聚焦。【参考方案2】:如果您在访问颜色时可能遇到问题,您可能希望将 colors.xml 添加到 values 文件夹中,其中包含一些您喜欢的颜色,例如:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background_pressed">#FFFF00</color> <!--yellow-->
<color name="background_normal">#808000</color> <!--olive-->
<color name="background_focused">#0000FF</color> <!--blue-->
</resources>
然后将另一个 xml 文件添加到您的可绘制文件夹中,使用您喜欢的任何名称
内容与之前的答案类似,但不是@drawable,而是调用@color
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@color/background_pressed" />
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@color/background_focused" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@color/background_pressed" />
<item
android:drawable="@color/background_normal" />
</selector>
现在您可以将视图的背景设置为可绘制对象
android:background="@drawable/background_click_state"
如果您想拥有所有 X11/w3c 颜色代码,请使用 this 帖子或仅使用此处提供的代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="White">#FFFFFF</color>
<color name="Ivory">#FFFFF0</color>
<color name="LightYellow">#FFFFE0</color>
<color name="Yellow">#FFFF00</color>
<color name="Snow">#FFFAFA</color>
<color name="FloralWhite">#FFFAF0</color>
<color name="LemonChiffon">#FFFACD</color>
<color name="Cornsilk">#FFF8DC</color>
<color name="Seashell">#FFF5EE</color>
<color name="LavenderBlush">#FFF0F5</color>
<color name="PapayaWhip">#FFEFD5</color>
<color name="BlanchedAlmond">#FFEBCD</color>
<color name="MistyRose">#FFE4E1</color>
<color name="Bisque">#FFE4C4</color>
<color name="Moccasin">#FFE4B5</color>
<color name="NavajoWhite">#FFDEAD</color>
<color name="PeachPuff">#FFDAB9</color>
<color name="Gold">#FFD700</color>
<color name="Pink">#FFC0CB</color>
<color name="LightPink">#FFB6C1</color>
<color name="Orange">#FFA500</color>
<color name="LightSalmon">#FFA07A</color>
<color name="DarkOrange">#FF8C00</color>
<color name="Coral">#FF7F50</color>
<color name="HotPink">#FF69B4</color>
<color name="Tomato">#FF6347</color>
<color name="OrangeRed">#FF4500</color>
<color name="DeepPink">#FF1493</color>
<color name="Fuchsia">#FF00FF</color>
<color name="Magenta">#FF00FF</color>
<color name="Red">#FF0000</color>
<color name="OldLace">#FDF5E6</color>
<color name="LightGoldenrodYellow">#FAFAD2</color>
<color name="Linen">#FAF0E6</color>
<color name="AntiqueWhite">#FAEBD7</color>
<color name="Salmon">#FA8072</color>
<color name="GhostWhite">#F8F8FF</color>
<color name="MintCream">#F5FFFA</color>
<color name="WhiteSmoke">#F5F5F5</color>
<color name="Beige">#F5F5DC</color>
<color name="Wheat">#F5DEB3</color>
<color name="SandyBrown">#F4A460</color>
<color name="Azure">#F0FFFF</color>
<color name="Honeydew">#F0FFF0</color>
<color name="AliceBlue">#F0F8FF</color>
<color name="Khaki">#F0E68C</color>
<color name="LightCoral">#F08080</color>
<color name="PaleGoldenrod">#EEE8AA</color>
<color name="Violet">#EE82EE</color>
<color name="DarkSalmon">#E9967A</color>
<color name="Lavender">#E6E6FA</color>
<color name="LightCyan">#E0FFFF</color>
<color name="BurlyWood">#DEB887</color>
<color name="Plum">#DDA0DD</color>
<color name="Gainsboro">#DCDCDC</color>
<color name="Crimson">#DC143C</color>
<color name="PaleVioletRed">#DB7093</color>
<color name="Goldenrod">#DAA520</color>
<color name="Orchid">#DA70D6</color>
<color name="Thistle">#D8BFD8</color>
<color name="LightGrey">#D3D3D3</color>
<color name="Tan">#D2B48C</color>
<color name="Chocolate">#D2691E</color>
<color name="Peru">#CD853F</color>
<color name="IndianRed">#CD5C5C</color>
<color name="MediumVioletRed">#C71585</color>
<color name="Silver">#C0C0C0</color>
<color name="DarkKhaki">#BDB76B</color>
<color name="RosyBrown">#BC8F8F</color>
<color name="MediumOrchid">#BA55D3</color>
<color name="DarkGoldenrod">#B8860B</color>
<color name="FireBrick">#B22222</color>
<color name="PowderBlue">#B0E0E6</color>
<color name="LightSteelBlue">#B0C4DE</color>
<color name="PaleTurquoise">#AFEEEE</color>
<color name="GreenYellow">#ADFF2F</color>
<color name="LightBlue">#ADD8E6</color>
<color name="DarkGray">#A9A9A9</color>
<color name="Brown">#A52A2A</color>
<color name="Sienna">#A0522D</color>
<color name="YellowGreen">#9ACD32</color>
<color name="DarkOrchid">#9932CC</color>
<color name="PaleGreen">#98FB98</color>
<color name="DarkViolet">#9400D3</color>
<color name="MediumPurple">#9370DB</color>
<color name="LightGreen">#90EE90</color>
<color name="DarkSeaGreen">#8FBC8F</color>
<color name="SaddleBrown">#8B4513</color>
<color name="DarkMagenta">#8B008B</color>
<color name="DarkRed">#8B0000</color>
<color name="BlueViolet">#8A2BE2</color>
<color name="LightSkyBlue">#87CEFA</color>
<color name="SkyBlue">#87CEEB</color>
<color name="Gray">#808080</color>
<color name="Olive">#808000</color>
<color name="Purple">#800080</color>
<color name="Maroon">#800000</color>
<color name="Aquamarine">#7FFFD4</color>
<color name="Chartreuse">#7FFF00</color>
<color name="LawnGreen">#7CFC00</color>
<color name="MediumSlateBlue">#7B68EE</color>
<color name="LightSlateGray">#778899</color>
<color name="SlateGray">#708090</color>
<color name="OliveDrab">#6B8E23</color>
<color name="SlateBlue">#6A5ACD</color>
<color name="DimGray">#696969</color>
<color name="MediumAquamarine">#66CDAA</color>
<color name="CornflowerBlue">#6495ED</color>
<color name="CadetBlue">#5F9EA0</color>
<color name="DarkOliveGreen">#556B2F</color>
<color name="Indigo">#4B0082</color>
<color name="MediumTurquoise">#48D1CC</color>
<color name="DarkSlateBlue">#483D8B</color>
<color name="SteelBlue">#4682B4</color>
<color name="RoyalBlue">#4169E1</color>
<color name="Turquoise">#40E0D0</color>
<color name="MediumSeaGreen">#3CB371</color>
<color name="LimeGreen">#32CD32</color>
<color name="DarkSlateGray">#2F4F4F</color>
<color name="SeaGreen">#2E8B57</color>
<color name="ForestGreen">#228B22</color>
<color name="LightSeaGreen">#20B2AA</color>
<color name="DodgerBlue">#1E90FF</color>
<color name="MidnightBlue">#191970</color>
<color name="Aqua">#00FFFF</color>
<color name="Cyan">#00FFFF</color>
<color name="SpringGreen">#00FF7F</color>
<color name="Lime">#00FF00</color>
<color name="MediumSpringGreen">#00FA9A</color>
<color name="DarkTurquoise">#00CED1</color>
<color name="DeepSkyBlue">#00BFFF</color>
<color name="DarkCyan">#008B8B</color>
<color name="Teal">#008080</color>
<color name="Green">#008000</color>
<color name="DarkGreen">#006400</color>
<color name="Blue">#0000FF</color>
<color name="MediumBlue">#0000CD</color>
<color name="DarkBlue">#00008B</color>
<color name="Navy">#000080</color>
<color name="Black">#000000</color>
</resources>
【讨论】:
以上是关于在按下时查看:按下时更改背景颜色?如何显示正在按下视图?的主要内容,如果未能解决你的问题,请参考以下文章