将ImageView和TextView并排居中

Dev*_*v01 0 android relativelayout android-layout

我想将所有内容布局居中居中,但这是我遇到的问题:

在此处输入图片说明

如您所见,我想将图像和文本置于其父对象的中心,但希望图像位于文本的左侧。

这是布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/app_light_green" >

    <ImageView
        android:id="@+id/toastImage"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_centerInParent="true"
        android:paddingTop="1dp"
        android:src="@drawable/exclamation" />

    <TextView
        android:id="@+id/toastText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:paddingBottom="3dp"
        android:paddingLeft="5dp"
        android:paddingRight="3dp"
        android:paddingTop="2dp"
        android:textColor="@color/app_darker_green"
        android:textSize="@dimen/sixteen_sp" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

str*_*s95 5

您可以使用水平的Linearlayout并在其中添加TextView和ImageView,然后在Relativelayout中添加Linearlayout并使其居中。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/toastImage"
        android:layout_width="24dp"
        android:src="@drawable/exclamation"
        android:layout_height="24dp"
        android:paddingTop="1dp" />

    <TextView
        android:id="@+id/toastText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingBottom="3dp"
        android:paddingLeft="5dp"
        android:paddingRight="3dp"
        android:paddingTop="2dp"
        android:textColor="@color/app_darker_green"
        android:textSize="@dimen/sixteen_sp"/>
</LinearLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)