Android 선택기 & 텍스트 색상
나는 심플함을 원합니다.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?
하나가 작동할 때까지 몇 가지 테스트를 수행했습니다. 그래서 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>
res/view/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>
여기서도 셀렉터가 답입니다.
소스에서 bright_text_dark_focused.xml을 검색하고 res/color 디렉토리에서 프로젝트에 추가한 다음 TextView에서 다음과 같이 참조합니다.
android:textColor="@color/bright_text_dark_focused"
목록 보기에서 선택할 수 있도록 하려면 다음 코드를 사용합니다.
<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"주.
다음은 목록의 항목과 동일하게 작동하는 구현입니다(최소 2.3).
res/message/list_video_message.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/color/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>
인res/color파일 "text_syslog.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()
}
오류가 있으면 죄송합니다, 제가 게시 전에 코드를 변경하고 확인하지 않았습니다.
다음은 선택기의 예입니다.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/topics/resources/drawable-resource.html#StateList
저는 이 이후로 더 검색하지 않고 항상 위의 솔루션을 사용했습니다. ;-)
그런데 오늘 우연히 뭔가를 발견하고 공유하려고 생각했습니다 :)
이 기능은 API 1에서 실제로 사용할 수 있으며 ColorStateList라고 하며, 여기서 다양한 위젯 상태에 색상을 제공할 수 있습니다(이미 알고 있음).
또한 여기에 매우 잘 문서화되어 있습니다.
탭에서 TextViews를 사용하는 경우 이 선택기 정의가 나에게 효과가 있었다면(Klaus 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
}
});
이것이 당신에게 도움이 되거나 아이디어를 주길 바랍니다.
언급URL : https://stackoverflow.com/questions/1219312/android-selector-text-color
'programing' 카테고리의 다른 글
| 부트스트랩 후 MariaDB가 정상이고 상태가 정상인지 확인합니다. (0) | 2023.08.28 |
|---|---|
| 도메인 간 Ajax 요청 후 쿠키 보관 (0) | 2023.08.28 |
| Javascript 객체의 필터 배열 (0) | 2023.08.28 |
| MySQL에서 제외하는 방법 (0) | 2023.08.28 |
| javascript / jQuery를 사용하여 data-* 속성 목록 가져오기 (0) | 2023.08.28 |