На Android я хотел бы создать кнопку, содержащую другие представления. Например, что-то вроде этого:
+---------------------------+
| Hello world! +-------+ |
| | image | |
| Some more info +-------+ |
+---------------------------+
Но я бы хотел, чтобы он был более гибким, чем этот конкретный пример. В идеале кнопка могла бы просто содержать ViewGroup, чтобы я мог реализовать ее макет в отдельном XML-файле. Однако, поскольку Button расширяет View, но не ViewGroup, это не представляется возможным.
Есть ли способ добиться этого с помощью стандартных компонентов Android, или мне нужно прибегнуть к написанию собственного класса кнопки?
Как запрошенный, некоторый пример XML, который делает трюк:
<?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>
Мне пришлось явно установить цвет для TextView, иначе они были бы почти нечитаемыми (по умолчанию они белые). Я откопал идентификатор из SDK: ... / platform / android-7 / data / res / values / public.xml
. Как ни странно, primary_text_light
дает черный текст, тогда как primary_text_dark
дает белый цвет ...