2011-06-02 33 views
16

Tôi muốn sử dụng bộ chọn cho bố cục và xem hình ảnh .. Nhưng nó không hoạt động đối với tôi. Khi tôi thay đổi chế độ xem hình ảnh thành nút hoặc nút hình ảnh, nó hoạt động tốt. Bất kỳ trợ giúp sẽ được đánh giá cao .. Cảm ơn trước ..

đây là XMLs tôi đã sử dụng ..không hoạt động với bố cục và chế độ xem hình ảnh

<?xml version="1.0" encoding="utf-8"?> 
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- When selected, use grey --> 
    <item 
     android:state_pressed="true" 
     android:drawable="@drawable/bg" 
     /> 
    <!--<item 

     android:drawable="@drawable/bg" 
     android:state_focused="true"/> 
    When not selected, use white--> 

    <item 
     android:drawable="@drawable/bg_01" /> 
</selector> 

-> The layout xml : 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    android:layout_height="fill_parent"> 
    <ImageButton 
     android:layout_width="100dp" 
     android:layout_height="50dp" 
     android:text="layout tester" 
     android:textColor="#000000" 
     android:textStyle="bold" 
     android:paddingTop="10dp" 
     android:src="@drawable/testselector" 
     android:layout_gravity="center_horizontal" /> 
    </LinearLayout> 
+0

Vì vậy, vấn đề chính xác của bạn là gì ...? –

+0

Tôi nghĩ bố cục không hỗ trợ bộ chọn (Tôi không chắc chắn). Tốt hơn hãy thử thuộc tính hình dạng.Tại sao bạn chọn bộ chọn? – David

+0

nếu ai đó muốn nhận được giải pháp đầy đủ, hãy kiểm tra kho lưu trữ này: https://github.com/shamanland/AndroidLayoutSelector có tùy chỉnh có thể nhấp/có thể kiểm tra '' 'LinearLayout''' như' '' ToggleButton''' –

Trả lời

59

Tôi đã gặp sự cố này ngay bây giờ. Bạn sẽ phải đặt LinearLayout thành có thể nhấp. Bạn có thể làm điều này trong XML với

android:clickable="true" 

Hoặc trong mã với

yourLinearLayout.setClickable(true); 
+1

Awesome :) đã có vấn đề này làm tôi thất vọng :) – Warpzit

+0

Tuyệt vời! Cảm ơn bạn rất nhiều! –

+0

Tuyệt vời !!! Cảm ơn rất nhiều người! :) –

3

thử này

<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" 
      android:state_pressed="false" 
      android:drawable="@drawable/lastread_ok" /> 
    <item android:state_focused="true" 
      android:state_pressed="true" 
      android:drawable="@drawable/lastread_ok" /> 
    <item android:state_focused="false" 
      android:state_pressed="true" 
     android:drawable="@drawable/lastread_ok_selected" /> 
    <item android:drawable="@drawable/lastread_ok" /> 
</selector> 

bạn có thể sử dụng hình ảnh của bạn thay vì lastread_ok & l astread_ok_selected

1

sử dụng id của ImageView thay vì bố trí tuyến tính và setclicklistener cho chính nó ImageView, nó sẽ làm việc !!

0

Đối với tôi, điều đó đã giúp không chỉ định thuộc tính android:clickable cho bố cục mà thay vào đó là ImageView. Tôi chỉ muốn hình ảnh có thể nhấp được, không phải bố cục chứa.

0

Bạn phải thay đổi src-background

hoặc

ImageButton để ImageView và alse viết android:clickable="true" trong ImageView thuộc tính.

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