Android Relative Layout can't make image on center

会有一股神秘感。 提交于 2019-12-07 15:46:27

I suggest using the TextView properties drawableLeft and drawableRight

android:drawableLeft="@drawable/whatever"
android:drawableRight="@drawable/arrow"
android:width="fill_parent"

That way and setting the TextView's gravity to center, everything should be ok

Also, using a single TextView instead of a RelativeLayout + 3 views is a big improvement in simplicity. Anyway, if you were to stick to 3 views, using LinearLayout instead of RelativeLayout is a good advice.

Why can't you use a LinearLayout?

Seems like this would work:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layercontainer" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp"> 
    <ImageView android:id="@+id/categoryIcon" 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:gravity="center_vertical" 
        android:layout_marginRight="15dp" 
        android:layout_marginTop="5dp" 
        android:layout_marginBottom="5dp" /> 
    <TextView android:id="@+id/categoryTitle" 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:gravity="center_vertical" 
        android:textStyle="bold" 
        android:textSize="22dp" 
        android:textColor="#000000" 
        android:layout_marginTop="5dp" 
        android:layout_marginBottom="5dp" /> 
    <ImageView 
        android:id="@+id/nextIcon" 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:gravity="center_vertical" 
        android:layout_marginTop="5dp" 
        android:layout_marginBottom="5dp"/> 
</LinearLayout> 

You should user LinearLayout for a simple xml structure

Because The LinearLayout is the most simple layout available in Android. A LinearLayout organizes layout components in a linear fashion horizontally or vertically

Creating Efficient Layouts says about performance of RelativeLayout and LinearLayout:

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