如何在左侧对齐(ImageView和TextView),在右侧对齐(ImageView和TextView)

时间:2017-02-03 16:13:01

标签: android android-layout android-fragments

我有一个标题栏,它有两个ImageViews

一个与左侧对齐,另一个与右侧对齐,如下所示

<RelativeLayout
    android:id="@+id/headersection"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#181b1c" >



    <ImageView
        android:id="@+id/img_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_margin="5dp"
        android:layout_marginRight="10dp"
        android:contentDescription="@string/img_des"
        android:src="@drawable/play" />



    <ImageView
        android:id="@+id/img_backicon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="5dp"
        android:layout_marginLeft="10dp"
        android:contentDescription="@string/img_des"
        android:src="@drawable/back" />
</RelativeLayout>

我想在每个图像旁边放置一个TextView,这样左边的ImageView旁边会有一个TextView(在左边ImageView左边对齐),右边的ImageView旁边会有一个TextView(右边​​对齐右边)正确的ImageView)...

我尝试了大约30种不同的编码方式,无法弄清楚

这是我想要做的一个例子

    <RelativeLayout
        android:id="@+id/headersection"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="#181b1c" >

<!-- NEED TO PUT A TEXTVIEW HERE THAT ALIGNS TO THE RIGHT IMAGE BELOW -->

    <TextView
        android:id="@+id/RIGHT_TEXT"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="RIGHT TEXT HERE"
        android:textColor="@color/Category_Title_Color"
        android:textSize="20sp" />

        <ImageView
            android:id="@+id/img_play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_margin="5dp"
            android:layout_marginRight="10dp"
            android:contentDescription="@string/img_des"
            android:src="@drawable/play" />


<!-- NEED TO PUT A TEXTVIEW HERE THAT ALIGNS TO THE LEFT IMAGE BELOW -->

    <TextView
        android:id="@+id/LEFT_TEXT"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="LEFT TEXT HERE"
        android:textColor="@color/Category_Title_Color"
        android:textSize="20sp" />

        <ImageView
            android:id="@+id/img_backicon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerHorizontal="true"
            android:layout_margin="5dp"
            android:layout_marginLeft="10dp"
            android:contentDescription="@string/img_des"
            android:src="@drawable/back" />
    </RelativeLayout>

当我尝试这样做时,文字与图像重叠或未正确对齐。

任何帮助将不胜感激

**** **** UPDATE 来自cricket_007的答案似乎对我来说很好......如果有人需要看到它,这里是完成的代码

<RelativeLayout
    android:id="@+id/headersection"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@color/colorbuttonbar" >


    <TextView
        android:id="@+id/img_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:gravity="center_vertical"
        android:layout_margin="5dp"
        android:layout_marginRight="10dp"
        android:text="MUSIC - "
        android:textStyle="bold"
        android:textColor="@color/Notification_Bar_Title_Color"
        android:drawableRight="@drawable/play" />


    <TextView
        android:id="@+id/img_backicon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerHorizontal="true"
        android:gravity="center_vertical"
        android:layout_margin="5dp"
        android:layout_marginLeft="10dp"
        android:text=" - GO BACK"
        android:textStyle="bold"
        android:textColor="@color/Notification_Bar_Title_Color"
        android:drawableLeft="@drawable/back" />






</RelativeLayout>

2 个答案:

答案 0 :(得分:4)

您可能根本不需要ImageView

TextView有两个左右图像属性:

android:drawableLeft

android:drawableRight

例如,

<TextView
        android:id="@+id/img_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_marginRight="10dp"
        android:text="MUSIC - "
        android:drawableRight="@drawable/play" /> <!-- See here -->

答案 1 :(得分:0)

您是否尝试过使用android:layout_toRightOfandroid:layout_toLeftOf以及android:layout_toStartOfandroid:layout_toEndOf

<RelativeLayout 
    android:id="@+id/headersection"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#181b1c">

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/textView" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/textView2" />

    <ImageView
        android:id="@+id/img_backicon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/back"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/textView"
        android:layout_toEndOf="@+id/textView" />

    <ImageView
        android:id="@+id/img_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/play"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/textView2"
        android:layout_toStartOf="@+id/textView2" />

</RelativeLayout>
相关问题