Android Relative Layout can't make image on center

空扰寡人 提交于 2019-12-08 06:15:26

问题


Hi i manage to make the icon center same line with the text in linear view, but I wanna add in a arrow image at the right side so i have to switch to relative view. As shown in the image, I can't manage to set the relative view left icon look like linear view.

EDIT
I want to arrow icon stay on the right.


Left image is linear, right is relative.
<RelativeLayout
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_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentBottom="true"
    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:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@id/categoryIcon"
    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="wrap_content"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"/>
</RelativeLayout>

回答1:


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.




回答2:


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> 



回答3:


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:



来源:https://stackoverflow.com/questions/10075475/android-relative-layout-cant-make-image-on-center

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