Android Studio의 탐색 브라우저 예제에서 시작합니다. 원하는 모든 항목을 사용자 지정하고 있습니다. 모두 괜찮지 만 탐색 창에서 선택한 항목 색상을 변경하는 방법을 찾을 수 없습니다. 지금은 녹색이며 변경할 수 없습니다.
선택기 드로어 블로 시도했지만 방법이 없었습니다. 전체 배경을 변경했지만 선택한 배경 만 변경하지 못했습니다.
멍청한 질문이라면 미안하지만 어제 Google에서 몇 시간을 보냈습니다.
마침내 나는 그것을 작동시킬 수 있었다. 문제는 "android.R.layout.simple_list_item_activated_1"이었습니다.
xml에 내장 된 안드로이드이므로 원하는 색상을 다룰 수 없습니다.
그래서 nav_drawer_layout.xml
"android.R.layout.simple_list_item_activated_1"파일과 같은 이름의 파일을 만들었습니다 .
그런 다음 android : background 속성을 다음과 같이 변경했습니다.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="@drawable/nav_drawer_colors"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
작성해야하는 nav_drawer_colors.xml
선택기 를 참조합니다 .
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/selected_drawer" android:state_activated="true" />
<item android:drawable="@color/selected_drawer" android:state_selected="true" />
<item android:drawable="@color/pressed_drawer" android:state_pressed="true" />
<item android:drawable="@color/backgroung_drawer" />
</selector>
selected_drawer, pressed_drawer 및 backgroung_drawer는 항상 values \ colors.xml에 정의되어 있습니다.
이제 모든 것이 괜찮습니다. 모든 곳에서 작동하지만 listview에서는 작동하지 않는 그림자 효과를 제외하고 ^^ 편집 : 조각 탐색 서랍 레이아웃 (fragment_navigation_drawer.xml)에서 배경색을 지정 <item android:drawable="@color/backgroung_drawer" />
하고 nav_drawer_colors.xml
파일 에서 제거 하십시오 :)
같은 문제를 가진 다른 사람들에게 도움이되기를 바랍니다. :)
코딩을 통해 ListView의 항목을 클릭하면 다음을 수행 할 수 있습니다.
int save = -1;
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
parent.getChildAt(position).setBackgroundColor(
Color.parseColor("#A9BCF5"));
if (save != -1 && save != position) {
parent.getChildAt(save).setBackgroundColor(
Color.parseColor("#d6e6ff"));
}
save = position;
});
선택기 사용 :
라는 이름의 하나 개의 파일 확인 select.xml
에 drawable
폴더를.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@color/green" />
<item android:drawable="@color/transparent" />
</selector>
이제이 파일을 xml 파일의 ListView에 대한 Listselector로 설정합니다. 처럼,
android:listselector="@drawable/select"
또는 배경으로도 사용
android:background="@drawable/select"
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다