Android layout: этот тег и его дочерние элементы могут быть заменены одним <TextView/"> и составным чертежом
Когда я запускаю макет в конкретном файле XML, я получаю следующее:
This tag and its children can be replaced by one <TextView/>
and a compound drawable
Что нужно сделать для следующего кода xml:
<LinearLayout android:id="@+id/name_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:background="@drawable/grouplist_single_left_grey_area" >
<ImageView android:id="@+id/photo_image"
android:layout_width="@dimen/thumbnail_width"
android:layout_height="@dimen/thumbnail_height"
android:paddingBottom="5dip"
android:paddingTop="5dip"
android:paddingRight="5dip"
android:paddingLeft="5dip"
android:layout_marginRight="5dip"
android:clickable="true"
android:focusable="true"
android:scaleType="fitCenter"
android:src="@*android:drawable/nopicture_thumbnail"
android:background="@drawable/photo_highlight" />
<TextView android:id="@+id/name"
android:paddingLeft="5dip"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Вот как это выглядит на экране:
![enter image description here]()
Значок камеры по умолчанию. Нажатие на это даст пользователю возможность выбрать другое изображение.
Ответы
Ответ 1
Чтобы развернуть ответ Ромен Гай, вот пример.
До:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="My Compound Button" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/my_drawable" />
</LinearLayout>
После:
<TextView
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My Compound Button"
android:drawableRight="@drawable/my_drawable" android:padding="5dp" />
Ответ 2
Объедините TextView
и ImageView
в один, используя методы TextView setCompoundDrawable*()
или используя android:drawableLeft
.
Ответ 3
Думал, что я попытаюсь получить дополнительные дополнительные возможности для этого: вы можете добавить отступы между изображением и текстом, используя android:drawablePadding
. fooobar.com/questions/24622/...
Ответ 4
Иногда можно заменить ImageView
(или несколько) и TextView
на один TextView
с помощью составных линий (-ов). Существует не так много параметров, которые могут быть применены к составной версии с использованием встроенного API и библиотека TextViewRichDrawable, но , если вы можете управлять одним TextView вместо использования LinearLayout вы обязательно должны использовать его.
Список атрибутов и параметров, которые могут быть применены к составным чертежам:
Размер: (ДА, действительно):
<com.tolstykh.textviewrichdrawable.TextViewRichDrawable
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text"
app:compoundDrawableHeight="24dp"
app:compoundDrawableWidth="24dp"/>
Даже установить векторный ресурс как возможно:
<com.tolstykh.textviewrichdrawable.TextViewRichDrawable
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text"
app:drawableTopVector="@drawable/some_vector_drawble"
app:drawableEndVector="@drawable/another_vector_drawable" />
Drawable Padding с использованием встроенного API android:drawablePadding
→ ссылка
Вот пример:
![textView rich drawable]()
Ответ 5
A LinearLayout
, который содержит ImageView
и a TextView
can
более эффективно обрабатываться как составной вариант (один
TextView
, используя drawableTop
, drawableLeft
, drawableRight
и/или drawableBottom
для рисования одного или нескольких изображений
рядом с текстом).
Если два виджета смещены друг от друга с полями, это
может быть заменен атрибутом drawablePadding
.
Там есть быстрое исправление для выполнения этого преобразования в Eclipse
плагин.
От: Официальные документы API для Android!
Ответ 6
Когда я следил за кодом выше, текст внутри TextView не устанавливается должным образом.
Вам нужно установить его силу тяжести, чтобы начать то, что показано в заданном вопросе.
Текстовое окно выглядит следующим образом:
<TextView
android:id="@+id/export_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawableLeft="@drawable/up_arrow"
android:drawableStart="@drawable/up_arrow"
android:gravity="center|start"
android:text="....."
android:textSize="@dimen/font_size15" >
</TextView>
Ответ 7
Если вы не хотите изменять ImageView и TextView, вы можете изменить версию в AndroidManifest.xml как:
<uses-sdk`
android:minSdkVersion="8"
android:targetSdkVersion="18"
/>
Если ваша версия - android: targetSdkVersion = "17", измените ее на "18".
Надеюсь, это исправит. Я сделал это и понял это правильно.
Ответ 8
Другой подход заключается в том, чтобы вставить ViewImage в другой LinearLayout (разрешить обработку его с помощью одного id):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/blue_3"
android:layout_gravity="center_horizontal"
android:paddingTop="16dp" />
</LinearLayout>
<TextView
android:id="@+id/tvPrompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="16dp"
android:text="@string/xy" />
Ответ 9
This tag and its children can be replaced by one <TextView/> and a compound drawable
<?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:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:contentDescription="."
android:padding="3dp"
android:src="@drawable/tab_home_btn">
</ImageView>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="首页"
android:textSize="10sp"
android:textColor="#ffffff">
</TextView>
</LinearLayout>