我想将textview放置在图像视图的右中心。我为此使用相对布局。使用相对布局,我可以将textview设置为图像视图的右侧,但是不能设置为中心。我的意思是这里的中心不在imageview的内部,我想将textview放置在imageview的右侧中心(imageview的外部中心)。我无法设置为center。任何人都请帮助我。我只想在相对布局中这样做。
这是我的xml,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_width="match_parent">
<TextView
android:id="@+id/name_text_view"
android:textSize="20sp"
android:layout_toRightOf="@id/image_view"
android:layout_width="wrap_content"
android:textColor="@color/black"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
<ImageView
android:id="@+id/image_view"
android:layout_marginLeft="30dp"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:id="@+id/button"
android:layout_centerHorizontal="true"
android:background="@color/blue"
android:textColor="@color/color_white"
android:layout_marginTop="20dp"
android:layout_below="@id/name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/val_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:layout_marginTop="20dp"
android:textSize="18sp"
android:layout_centerHorizontal="true"
android:layout_below="@id/button"/>
</RelativeLayout>
您可以在文本视图中添加它
android:layout_centerInParent="true"
您可以使用以下代码完成此操作:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp">
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="center"
android:src="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/image_view"
android:layout_centerInParent="true"
android:text="hello world"
android:textColor="@color/black"
android:textSize="12sp" />
</RelativeLayout>
只需添加
android:layout_alignRight="@+id/image_view"
android:layout_centerInParent="true"
在您的TextView
中
使用此代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<ImageView
android:id="@+id/image_view"
android:layout_marginLeft="30dp"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="@+id/name_text_view"
android:textSize="20sp"
android:layout_toRightOf="@id/image_view"
android:layout_width="wrap_content"
android:textColor="@color/black"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_centerHorizontal="true"
android:background="@color/blue"
android:textColor="@color/color_white"
android:layout_marginTop="20dp"
android:layout_below="@id/name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/val_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:layout_marginTop="20dp"
android:textSize="18sp"
android:layout_centerHorizontal="true"
android:layout_below="@id/button"/>
</RelativeLayout>