Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a LinearLayout and inside, a ImageView and TextView. I want to align the center of the LinearLayout the ImageView and the TextView below the ImageView.

I only get the top align ImageView in LinearLayout. How I can do?

       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:layout_margin="8dp"
            android:gravity="center_horizontal"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/boton"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_gravity="center"
                android:background="@drawable/plano"
                android:gravity="center" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Plano"
                android:textSize="23sp"
                android:textStyle="bold"
                />

        </LinearLayout>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
411 views
Welcome To Ask or Share your Answers For Others

1 Answer

I'll show you how to do the same taking advantage of a RelativeLayout and a TextView's compoundDrawable:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:layout_margin="8dp"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:drawableTop="@drawable/plano"
        android:drawablePadding="8dp"
        android:text="Plano"
        android:textSize="23sp"
        android:textStyle="bold"
    />
</RelativeLayout>

Compound drawables help keeping down the View count, and therefore intrinsically speed up the layout

The key is android:drawableTop="@drawable/plano" This replaces the top image.
You can also set a Right, Left, Bottom side image.
Even all together.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...