Align ImageView & TextView inside LinearLayout

╄→尐↘猪︶ㄣ 提交于 2019-12-26 16:56:12

问题


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>

回答1:


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.




回答2:


You have to put the "layout_gravity" field as "center_horizontal" but the two widgets (ImageView, TextView).

Here code:

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

  <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:layout_gravity="center_horizontal"
      android:src="@drawable/ic_launcher"/>
  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceSmall"
      android:text="Small Text"
      android:id="@+id/textView" 
      android:layout_gravity="center_horizontal"/>

</LinearLayout>


来源:https://stackoverflow.com/questions/25431092/align-imageview-textview-inside-linearlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!