[英]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.