2013-01-25 12 views
7

Iは、画像ビューとボタンをユーザがクリックimages.when押したこのRelativelayoutをクリック可能にするにはどうすればいいですか?

<?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

<?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> 

ようなレイアウトを持っています画像ビューとボタン(押された画像を表示する)の両方を押すことを望んでいるが、押された画像は表示されない。これを達成する方法は?問題はどこだ?

ありがとうございます。

+1

はあなたのRelativeLayoutにOnClickListenerを添付しました問題を解決しないでしょうか? – Shark

+0

彼らはお互いの上に積み重なっていますか? – njzk2

+1

画像ビューをボタンで置き換え、ボタンに重複した親状態を追加する – njzk2

答えて

4

私はそれが

((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 RelayLayoutはFrameLayout内にあります。

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

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

"" RelativeLayoutをタイプに解決できない "というメッセージが表示されている場合は、次のものを一番上にインポートしてくださいあなたのJavaファイル:import android.widget.RelativeLayout; and android.view.View; – jwinn

+1

あなたは 'android:clickable =" true "'を設定できませんか? –