How do you make an entire control clickable in android?
I have a custom control in android that has a text view, some images, etc.
I want to make the entire control clickable.
I declared the layout with:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" 开发者_如何学Python
android:clickable="true"
android:layout_width="fill_parent"
android:layout_height="50px"
android:layout_gravity="center_vertical">
even though it has that android:clickable attribute set to true, the click event never fires.
am I doing something wrong?
In your Main Activity code , do something like this
public class TestActivity extends Activity implements
OnTouchListener, OnLongClickListener, OnKeyListener,
OnClickListener, OnFocusChangeListener{
CustomUI = (CustomView) findViewById(R.id.custom_ui);
CustomUI.addListener(this);
CustomUI.setOnClickListener(this);
CustomUI.setOnFocusChangeListener(this);
CustomUI.setOnLongClickListener(this);
CustomUI.setOnTouchListener(this);
CustomUI.setOnKeyListener(this);
}
Also you can't put attributes in your Relative layout , you have to include your custom ui it this layout ... something like
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<com.company.some.CustomView
android:id="@+id/custom_ui"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
/>
</RelativeLayout>
精彩评论