Android的选择和文字颜色
-
10-07-2019 - |
题
我想要一个简单的TextView
的运行方式simple_list_item_1
在ListView
一样。下面是XML:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:gravity="center" android:focusable="true"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="@android:drawable/list_selector_background" />
一切工作除了文本颜色(果然)不聚焦状态发生变化。我如何将其更改为textAppearanceLargeInverse
?
解决方案 2
和选择器是答案这里。
搜索来源bright_text_dark_focused.xml,添加到您的项目RES /彩色目录下,然后从TextView的参考为
android:textColor="@color/bright_text_dark_focused"
其他提示
我做一些测试,直到一个工作,所以: RES /颜色/ button_dark_text.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="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
RES /布局/ view.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="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT"
android:textColor="@color/button_dark_text" />
</LinearLayout>
这是我的实现,它的行为完全如在列表中(至少在2.3)项目
RES /布局/ list_video_footer.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/list_video_footer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:drawable/list_selector_background"
android:clickable="true"
android:gravity="center"
android:minHeight="98px"
android:text="@string/more"
android:textColor="@color/bright_text_dark_focused"
android:textSize="18dp"
android:textStyle="bold" />
</FrameLayout>
RES /颜色/ bright_text_dark_focused.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#444"/>
<item android:state_focused="true" android:color="#444"/>
<item android:state_pressed="true" android:color="#444"/>
<item android:color="#ccc"/>
</selector>
为了使其上选择在列表视图中工作使用以下代码:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#fff"/>
<item android:state_activated="true" android:color="#fff"/>
<item android:color="#000" />
</selector>
显然,关键是state_activated="true"
状态。
下面是选择器的例子。如果你使用Eclipse,当你点击Ctrl键和空格都不会暗示什么:/你必须键入
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_default_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/btn_default_selected"
android:state_focused="true"
android:state_enabled="true"
android:state_window_focused="true" />
<item android:drawable="@drawable/btn_default_normal" />
可以看作参考;
http://developer.android.com/guide/主题/资源/抽拉-resource.html#StateList
我一直用上述溶液而无需搜索后于此。 ; - )
不过,今天我碰到的东西和分享它的思想。 :)
此特征确实是可从API 1和被称为的 ColorStateList 强>,其中我们可以提供一种颜色窗口小部件的各种状态(如我们已经知道)。
这也是非常有据可查的,。
如果在标签此选择定义为我工作使用TextViews(试过克劳斯Balduino的,但事实并非如此):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item
android:state_selected="true"
android:state_focused="false"
android:state_pressed="false"
android:color="#000000" />
<!-- Inactive tab -->
<item
android:state_selected="false"
android:state_focused="false"
android:state_pressed="false"
android:color="#FFFFFF" />
</selector>
你试过setOnFocusChangeListener
?在处理程序,你可以改变文本的外观。
例如:
TextView text = (TextView)findViewById(R.id.text);
text.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
((TextView)v).setXXXX();
} else {
((TextView)v).setXXXX();
}
}
});
您可以再申请要在它的集中与否的任何变化。您也可以使用ViewTreeObserver侦听全球焦点的变化。
例如:
View all = findViewById(R.id.id_of_top_level_view_on_layout);
ViewTreeObserver vto = all.getViewTreeObserver();
vto.addOnGlobalFocusChangeListener(new ViewTreeObserver.OnGlobalFocusChangeListener() {
public void onGlobalFocusChanged(
View oldFocus, View newFocus) {
// xxxx
}
});
我希望这可以帮助或者给你的想法。
在res/color
代替文件 “text_selector.xml”:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/blue" android:state_focused="true" />
<item android:color="@color/blue" android:state_selected="true" />
<item android:color="@color/green" />
</selector>
然后,在TextView
使用它:
<TextView
android:id="@+id/value_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text"
android:textColor="@color/text_selector"
android:textSize="15sp"
/>
和代码,你需要设置点击收听。
private var isPressed = false
private fun TextView.setListener() {
this.setOnClickListener { v ->
run {
if (isPressed) {
v.isSelected = false
v.clearFocus()
} else {
v.isSelected = true
v.requestFocus()
}
isPressed = !isPressed
}
}
}
override fun onResume() {
super.onResume()
textView.setListener()
}
override fun onPause() {
textView.setOnClickListener(null)
super.onPause()
}
很抱歉,如果有错误,我出版之前改变一个代码和未检查。