2013-01-25 13 views
7

Tôi có một bố cục nhưCách tạo Relativelayout có thể nhấp?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:addStatesFromChildren="true" 
    android:clickable="true" 
    android:duplicateParentState="true" 
    android:focusable="true" 
    android:paddingBottom="10dip" > 

    <ImageView 
     android:id="@+id/imagView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:focusable="false" 
     android:background="@drawable/imageView_bg" 
     android:focusableInTouchMode="false" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="18dip" 
     android:background="@drawable/button_bg" 
     android:clickable="false" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:gravity="left|center_vertical" 
     android:paddingLeft="25dip" 
     android:textColor="#ffffff" 
     android:textSize="20dip" /> 

</RelativeLayout> 

button_bg.xml này

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:drawable="@drawable/btn_pressed" 
      android:state_pressed="true"/> 
     <item android:drawable="@drawable/btn_normal"/> 

    </selector> 

imageView_bg.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/img_pressed" 
     android:state_pressed="true"/> 
    <item android:drawable="@drawable/img_normal"/> 

</selector> 

Quan điểm ảnh và nút đã ép images.when người dùng nhấp vào xem, muốn nhấn cả hai lần xem hình ảnh và nút (Hiển thị hình ảnh được nhấn). Nhưng nó không hiển thị hình ảnh được nhấn. Làm thế nào để đạt được điều này? Vấn đề ở đâu?

Cảm ơn trước

+1

Bạn có đính kèm một OnClickListener để RelativeLayout của bạn? – Shark

+0

chúng có được xếp chồng lên nhau không? – njzk2

+1

thay thế chế độ xem hình ảnh theo bố cục, trong đó bạn đặt nút và thêm trạng thái gốc trùng lặp vào nút – njzk2

Trả lời

4

Tôi nghĩ rằng nó giải quyết vấn đề

((RelativeLayout)findViewById(R.id.mainLayout)).setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View view) { 
    ViewGroup viewGroup = (ViewGroup) view; 
    for (int i = 0; i < viewGroup .getChildCount(); i++) { 

     View viewChild = viewGroup .getChildAt(i); 
     viewChild.setPressed(true); 

      } 
    } 
}); 
0

Simple.Place RelativeLayout bên trong FrameLayout.

13
RelativeLayout rl = (RelativeLayout)findViewById(R.id.mainLayout); 

rl.setOnClickListener(new View.OnClickListener(){ 
    public void onClick(View v) {} 
}); 
+0

Nếu bạn nhận được "" RelativeLayout không thể được giải quyết thành loại ", hãy đảm bảo bạn đang nhập thông tin sau ở đầu của tệp java của bạn: nhập android.widget.RelativeLayout và nhập android.view.View; – jwinn

+1

Bạn không thể đặt 'android: clickable =" true "'? –