Howdy_McGee
Howdy_McGee

Reputation: 10635

This Tag And Its Children Can Be Replaced by One <TextView/> And a Compound Drawable

I looked at the similar question and used it's answer, but if I combine them into 1 TextView and put android:drawableTop= It shows up very strange where as I have it now it just stacks the image on top of the text which is what I want.

Original

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Navigation" >

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="@string/logo_name"
        android:src="@drawable/generic_logo" />

    <TextView
        android:id="@+id/about"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/about"
        android:textSize="55sp"
        android:onClick="onClick"
        android:clickable="true" />

</LinearLayout>

With Compound

    <TextView
        android:id="@+id/about"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/about"
        android:textSize="55sp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableTop="@drawable/generic_logo" />

</LinearLayout>

The Compound stretches the image beyond the parameters of my emulated android. How can I fix this and also get rid of the warning?

Upvotes: 1

Views: 3903

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93561

So these two are actually doing quite different things, so far as measurement and layout go. The first is figuring out how much room is needed for the image, putting it there, figuring out how much room is needed for the text, and putting it below the image.

The second is figuring out how much room it needs for the text. Its then drawing the image in the top part of the background, scaling it to fit the entire space used by the text. Then it draws the text on top of it.

If you want to have the image not stretched, use the first version. The second version is just wrong. Compound drawables are used for things like 9 patches where a single background image is broken into parts.

If neither of these are doing what you really want, explain what that is and we'll see if we can figure out how to do it.

Upvotes: 2

Related Questions