[英]How to align (an ImageView and a TextView) to the left and (an ImageView and a TextView) to the right
I have a header bar which has two ImageViews 我有一个标题栏,其中有两个ImageViews
One is aligned to the left and one is aligned to the right, like below 一个对齐到左边,一个对齐到右边,如下所示
<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>
I want to put a TextView beside each image so the left ImageView would have a TextView beside it (aligned to the left right beside the left ImageView) and the right ImageView would have a TextView beside it (aligned to the right right beside the right ImageView)... 我想在每个图像旁边放置一个TextView,以便左侧ImageView在其旁边具有一个TextView(在左侧ImageView旁边与左侧对齐),右侧ImageView在其旁边具有一个TextView(在右侧ImageView旁边与右侧对齐) )...
I have tried about 30 different ways of coding and can't figure it out 我尝试了大约30种不同的编码方式,但无法弄清楚
Here is an example of what I am trying to do 这是我正在尝试做的一个例子
<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>
When I tried to do that, the text kept overlapping the images or not aligning properly. 当我尝试这样做时,文本始终与图像重叠或对齐不正确。
Any help would be appreciated 任何帮助,将不胜感激
****UPDATE**** the answer from cricket_007 seemed to work fine for me... here is the finished code if anyone needs to see it **** 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>
You probably don't need an ImageView
at all. 您可能根本不需要ImageView
。
TextView
has two properties for left and right images: TextView
具有左右图像的两个属性:
For example, 例如,
<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 -->
Have you tried using android:layout_toRightOf
and android:layout_toLeftOf
as well as android:layout_toStartOf
and android:layout_toEndOf
: 您是否尝试过使用android:layout_toRightOf
和android:layout_toLeftOf
以及android:layout_toStartOf
和android: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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.