[英]Is there a way to align TextView vertically center with ImageView in RelativeLayout
I have a RelativeLayout
with ImageView
and TextView
on the left and ImageView
and TextView
on the right. 我有一个
RelativeLayout
用ImageView
和TextView
左侧和ImageView
和TextView
右侧。
but they are not aligning well with the ImageView. 但它们与ImageView的对齐方式不太好。 I want it to align vertically center to the image next to the text view
我希望它与文本视图旁边的图像垂直居中对齐
This is how it looks at the moment: 现在是这样的:
The RelativeLayout I'm using is this: 我正在使用的RelativeLayout是这样的:
Question 题
How can I make the TextView
align well with the ImageView
如何使
TextView
与ImageView
对齐
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/leftIcon"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/trash" />
<TextView
android:id="@+id/tv1"
android:layout_toRightOf="@+id/leftIcon"
android:layout_toLeftOf="@+id/iv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:text="some text 1"
android:layout_marginLeft="5dp"
android:textColor="#000000"
android:textStyle="bold"/>
<ImageView
android:id="@+id/iv2"
android:layout_toLeftOf="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/trash" />
<TextView
android:layout_alignParentRight="true"
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:textColor="#000000"
android:text="some text 2"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:layout_marginRight="25dp" />
</RelativeLayout>
hi try this code i think it will help you :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_alignParentLeft="true"
android:orientation="horizontal" >
<ImageView
android:id="@+id/leftIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_delete" />
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="some text 1"
android:textColor="#ffffff"
android:textSize="14dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_delete" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="25dp"
android:text="some text 2"
android:textColor="#ffffff"
android:textSize="14dp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
// Try this way,hope this will help you to solve your problem...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<ImageView
android:id="@+id/leftIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:adjustViewBounds="true"/>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="some text 1"
android:layout_marginLeft="5dp"
android:textColor="#000000"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|right">
<ImageView
android:id="@+id/iv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:adjustViewBounds="true"/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
android:text="some text 2"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:layout_marginRight="25dp" />
</LinearLayout>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/leftIcon"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/iv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/tv2"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv2"
android:layout_toRightOf="@+id/leftIcon"
android:text="some text 1"
android:textColor="#000000"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="26dp"
android:text="some text 2"
android:textColor="#000000"
android:textSize="14dp"
android:textStyle="bold" />
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.