2011-12-15 65 views
7

Tôi phải tạo một màu nổi bật khác cho mục ListView. Tôi sử dụng bộ chuyển đổi tùy chỉnh cho các hạng mục, và tôi đã mã sau:Cách đặt màu đánh dấu cho mục ListView?

Layout:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF" 
    android:weightSum="1.0" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_weight="0.13" 
     android:background="@drawable/booklist_header" 
     android:orientation="horizontal" 
     android:layout_height="0dp" > 

     <ImageView 
      android:id="@+id/imageViewBookListBack" 
      android:layout_width="wrap_content" 
      android:layout_gravity="center" 
      android:layout_height="wrap_content" 
      android:src="@drawable/back_button" /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="fill_parent" 
     android:layout_weight="0.77" 
     android:layout_height="0dp" > 

     <ListView 
      android:id="@+id/listViewCurrentList" 
      android:listSelector="@drawable/selector" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     </ListView> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayoutBooklistAdwhirl" 
     android:layout_width="fill_parent" 
     android:layout_weight="0.1" 
     android:layout_height="0dp" > 
    </LinearLayout> 

</LinearLayout> 

Mã cho bố cục mục:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/imageViewBookListItemImage" 
     android:layout_width="120dp" 
     android:layout_marginTop="15dp" 
     android:layout_height="120dp" /> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="135dp" 
     android:layout_marginTop="15dp" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textViewBookListItemTitle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Medium Text" 
      android:textColor="#000000" 
      android:textStyle="bold" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <TextView 
      android:id="@+id/textViewBookListItemAuthor" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Medium Text" 
      android:textColor="#000000" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

    </LinearLayout> 

</LinearLayout> 

Mã cho selector:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" android:drawable="@color/green" /> <!-- focused --> 
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/green" /> <!-- focused and pressed--> 
    <item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed --> 
    <item android:drawable="@color/green" /> <!-- default --> 
</selector> 

Mã cho color.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="green">#006400</color> 
    <color name="white">#FFFFFF</color> 


</resources> 

Nhưng tôi gặp sự cố: Mục ListView không thay đổi màu sắc bằng cách nhấp! Nó luôn có màu trắng. Tôi đã phạm sai lầm ở đâu?

Trả lời

23

Đây là cách bạn làm điều đó:

Thứ nhất, trong ListView của bạn, đặt sau:

android:listSelector="#00000000" 

Điều này làm cho listSelector của bạn (màu mà bạn thường thấy khi bạn nhấp vào listview) trong suốt.

Tiếp theo, thiết lập các LinearLayout của bố cục mục của bạn như thế này:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/selector" 
    android:orientation="horizontal" > 

tôi đã cùng một vấn đề một vài ngày trước và nó đã cho tôi lứa tuổi để con số này ra. Hy vọng nó hoạt động cho bạn!

+1

Cảm ơn bạn rất nhiều !!! – user1078760

+4

Có hai dòng chứa 'android: background'. Vui lòng sửa –

2

Bạn có thể thử thêm mục này: android: drawSelectorOnTop = "true" vào định nghĩa của ListView.

Hy vọng điều này sẽ giúp bạn!

P.S. Bạn có thể tối ưu hóa bố cục chính của mình và xóa các bố cục không cần thiết. Bạn có thể sử dụng layoutopt cho việc này.

+0

Cảm ơn bạn. Nhưng bây giờ khi tôi nhấp vào mục hơn toàn bộ ListView thay đổi màu sắc cho màu xanh lá cây, nhưng tôi chỉ cần 1 mục. Làm thế nào tôi có thể sửa chữa nó? – user1078760

+0

Hmm, cho vấn đề này bạn có thể kiểm tra ở đây cho một giải pháp: http://stackoverflow.com/questions/2562051/listview-item-background-via-custom-selector. Tôi cũng sẽ kiểm tra và quay lại với bạn càng sớm càng tốt. –

Các vấn đề liên quan