2014-03-24 50 views
0

设置:的Android LinearLayout中,最后添加的孩子偷点击onClickListener

我有了一个TextView和里面的ImageView的(我试图解决这个问题被注释掉)定制的LinearLayout:

public class MyTextView extends LinearLayout { 

    private final TextView textView; 
    private final ImageView imageView; 

    public MyTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setOrientation(VERTICAL); 

     textView = new TextView(context); 
     imageView = new ImageView(context); 

     LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 
       LayoutParams.WRAP_CONTENT); 
     imageView.setLayoutParams(layoutParams); 
     imageView.setImageResource(R.drawable.img); 

     /* imageView.setDuplicateParentStateEnabled(true); 
     textView.setDuplicateParentStateEnabled(true); */ 

     /* imageView.setFocusable(false); 
     textView.setFocusable(false); 
     textView.setTextIsSelectable(false); */ 

     addView(textView); 
     addView(imageView); 

     /* this.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS); 
     this.setClickable(true); */ 

    } 
} 

我,包括一个更大的布局这个定制的LinearLayout:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <my.package.MyTextView 
     android:id="@+id/textView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

</LinearLayout> 

我设置的onClickListener这方法:

final MyTextView textView = (MyTextView) view.findViewById(R.id.textView); 
textView.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     // Code 
    } 
}); 

我的问题:

的onClickListener如果我挖掘ImageView的,而不是只TextView的火灾。如果我点击LinearLayout的任何部分,我希望它能够工作。

我不明白为什么这不起作用,甚至没有例如。 setDescendantFocusability设置为FOCUS_BLOCK_DESCENDANTS,并希望得到解释。非常感谢。

回答

相关问题