简体   繁体   中英

Align TextView to vertical center of ImageView despite the text size?

I have an ImageView and I want it to be followed by a TextView that will always align to it's vertical center despite of the number of lines of text.

Here's what i'm talking about: http://i.imgur.com/fsty3hN.jpg

Since I haven't found anything related to align in vertical center the only property that the text has regarding the alignment is toRightOf the ImageView.

Can someone help me on this matter?

Thx

Instead of an ImageView and a TextView , you can use a compound drawable, which is basically a View that will mix both. In this View , you can specify the gravity and where the image will be located :

<TextView
     android:id="@+id/your_id"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:drawableLeft="@drawable/your_image"
     android:gravity="center"
     android:text="@string/text" />

设置存在图像和文本的布局的重力。您还可以将textview的重力设置为垂直居中,水平居中或居中

Try using TableLayout and give gravity to the TextView as center

<TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

         <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

         <ImageView 
            android:id="@+id/eimaget_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           android:background="@drawable/your_image"
            />
        <TextView 
            android:id="@+id/textt_id"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text="text is here"
            android:layout_weight="1"
            />
        </TableRow>
        </TableLayout>

Hope it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM