问题描述
|
试图让onclick侦听器在线性布局上工作,但从未调用过:(。已启用clickable和focsuable(两种模式),但仍无法使Click侦听器响应。平台详细信息:Android 3.0。
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:id=\"@+id/menu_items_button\"
android:layout_width=\"0dip\"
android:layout_height=\"fill_parent\"
android:layout_weight=\"1\"
android:orientation=\"vertical\"
android:gravity=\"center_horizontal\"
android:paddingTop=\"@dimen/gen_margin_xsmall\"
android:paddingBottom=\"@dimen/gen_margin_xsmall\"
android:background=\"@drawable/rule_bg_menu_button\"
android:clickable=\"true\"
android:focusableInTouchMode=\"true\"
>
<ImageView
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:src=\"@drawable/menu_items\"
android:tag=\"image\"
/>
<TextView
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:tag=\"text\"
android:text=\"@string/menu_items_icon_txt\"
style=\"@style/textdisplay.mediumLarge\"
/>
</LinearLayout>
并在代码中添加事件监听器
_itemsButton = (LinearLayout) menu.findViewById(R.id.menu_items_button);
final Intent itemsIntent = new Intent(this,ItemsActivity.class);
_itemsButton.setonClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(itemsIntent); //Never called!
}
}
);
我这样做而不使用“图像”按钮的原因是因为““按钮””的背景是基于状态的(渐变),但是图像也是基于状态的,以便在单击/聚焦时将两者结合在一起,我使用了一个其自身具有ImageView的linearlayout。有关clickListener为什么不能在linearLayout上工作的任何建议?
谢谢
解决方法
单击是否转到ImageView而不是LinearLayout?尝试在便签区域中单击(如果有的话),或尝试将Click侦听器放在ImageView1上。
,(将回答添加为新答案,以便可以使用PRE标签。)
简单的方法是在图像视图和文本视图上设置相同的单击侦听器。
View.OnClickListener activityLauncher = new View.OnClickListener() {... }
layout.setOnClickListener(activityLauncher);
imageView.setOnClickListener(activityLauncher);
textView.imageView.setOnClickListener(activityLauncher);
,LinearLayout
的宽度设置为\“ 0dip \”,屏幕上什么也看不到。
如果宽度更改为\“ FULL_PARENT \”,则可以。请再次仔细检查您的代码。