[英]How to align (an ImageView and a TextView) to the left and (an ImageView and a TextView) to the right
我有一个标题栏,其中有两个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>
您可能根本不需要ImageView
。
TextView
具有左右图像的两个属性:
例如,
<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 -->
您是否尝试过使用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.