开发者

LinearLayout's click listener is never called

Trying to get an onclick listener working on a linearlayout but its never called :(. Have enabled clickable and focsuable (both modes) and still cant get the click listener to respond. Platform details: Android 3.0.. Any help?? Code below

           <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"
                    a开发者_如何学编程ndroid: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>

and in the code to add the event listener

_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!
                }
            }
        );

The reason I'm doing this and not using an Image button instead is because the background of the "button" is state based (gradient changes) but also the image and in order to combine to the two on click / on focus, I used a linearlayout which has an ImageView in itself.. any suggestions on why the clickListener is not working on the linearLayout?

thx


Did the click go to the ImageView instead of the LinearLayout? Try clicking in the pad area (if any) or try putting the click listenner on the ImageView1.


(adding my response as a new answer so that I can use the PRE tag.)

The easy way is to set the same click listener on the image view and the text view.

View.OnClickListener activityLauncher = new View.OnClickListener() {... }
layout.setOnClickListener(activityLauncher);
imageView.setOnClickListener(activityLauncher);
textView.imageView.setOnClickListener(activityLauncher);


The width of your LinearLayout is set to "0dip", you should see nothing on the screen.

If the width is changed to "FULL_PARENT", that works. Please check your code carefully again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜