How to create a button containing multiple views?

On Android, I would like to create a button containing some other views. For example, something like this:

+---------------------------+
| Hello world!    +-------+ |
|                 | image | |
| Some more info  +-------+ |
+---------------------------+

But I would like it to be more flexible than this specific example. Ideally, the button will simply contain a ViewGroup so that I can implement its layout in a separate XML file. However, since Button extends View, but not ViewGroup, this is not possible.

Is there a way to achieve this using standard Android components, or do I need to resort to writing a custom button class?


As per the request, in some XML example that does the trick:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:focusable="true"
    android:clickable="true"
    android:background="@android:drawable/btn_default">
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/primary_text_light"/>
    <TextView
        android:id="@+id/additional_line_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/primary_text_light"/>
    <TextView
        android:id="@+id/additional_line_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/primary_text_light"/>
</LinearLayout>

TextViews , ( ). SDK: .../platforms/android-7/data/res/values/public.xml. , primary_text_light , primary_text_dark ...

+5
3

android:background @android:drawable/btn_default. , clickable focusable, .

+4

, ImageButton? , , .

[] " " . , , OnTouchListener .

+1

. RelativeLayout TextView a ImageView.

, , , ViewGroup extends View View . .

, , @android:drawable/btn_default , @ognian.

0

All Articles