Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

android - Align ImageView & TextView inside LinearLayout

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...