yan*_*nko 181 android textview android-selector
我想要一个简单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" />
Run Code Online (Sandbox Code Playgroud)
一切都有效,除了(预期)在聚焦状态下不会改变的文本颜色.如何将其更改为textAppearanceLargeInverse?
小智 395
我做了几次测试直到一个工作,所以:res/color/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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
yan*_*nko 79
而选择器也是这里的答案.
在源代码中搜索bright_text_dark_focused.xml,在res/color目录下添加到项目中,然后从TextView引用
android:textColor="@color/bright_text_dark_focused"
Run Code Online (Sandbox Code Playgroud)
kre*_*ker 31
这是我的实现,其行为与列表中的项目完全相同(至少在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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
Man*_*rig 26
为了使其在列表视图中进行选择,请使用以下代码:
<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>
Run Code Online (Sandbox Code Playgroud)
显然关键是state_activated="true"国家.
放置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>
Run Code Online (Sandbox Code Playgroud)
然后在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"
/>
Run Code Online (Sandbox Code Playgroud)
在代码中,您需要设置一个点击监听器。
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()
}
Run Code Online (Sandbox Code Playgroud)
抱歉,如果有错误,我在发布之前更改了代码,但没有检查。
这是选择器的示例。如果您使用 eclipse ,当您单击 ctrl 和 space 时它不会提出任何建议:/您必须键入它。
<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" />
Run Code Online (Sandbox Code Playgroud)
您可以参考一下;
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
| 归档时间: |
|
| 查看次数: |
250659 次 |
| 最近记录: |