如何在相对布局中将 Textview 放置在 Imageview 的右侧和中心
How to place a Textview to the right and center of the Imageview in Relative Layout
我想在图像视图的右侧和重心位置放置一个文本视图。我为此使用相对布局。使用相对布局,我能够将 Textview 设置到图像的右侧 view.But 无法将其设置到中心。我的意思是这里的中心不在 Imageview 内部,我想将 Textview 放在 Imageview 的右中心(Imageview 中心之外)。我无法设置为中心。
任何人都请帮助我 out.I 只想在相对布局中这样做。
这是我的 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>
使用此代码:
<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>
你想要这个视图吗?然后试试这个代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_width="match_parent">
<LinearLayout
android:id="@+id/leaner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/image_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:contentDescription="TODO"
app:srcCompat="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:textColor="@android:color/black"
android:textSize="20sp" />
<TextView
android:id="@+id/val_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Value"
android:textColor="@android:color/black"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/leaner1"
android:layout_marginTop="1dp"
android:background="@android:color/holo_blue_dark"
android:text="button"
android:textColor="@android:color/white" />
</RelativeLayout>
我想在图像视图的右侧和重心位置放置一个文本视图。我为此使用相对布局。使用相对布局,我能够将 Textview 设置到图像的右侧 view.But 无法将其设置到中心。我的意思是这里的中心不在 Imageview 内部,我想将 Textview 放在 Imageview 的右中心(Imageview 中心之外)。我无法设置为中心。 任何人都请帮助我 out.I 只想在相对布局中这样做。
这是我的 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>
使用此代码:
<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>
你想要这个视图吗?然后试试这个代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_width="match_parent">
<LinearLayout
android:id="@+id/leaner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/image_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:contentDescription="TODO"
app:srcCompat="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:textColor="@android:color/black"
android:textSize="20sp" />
<TextView
android:id="@+id/val_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Value"
android:textColor="@android:color/black"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/leaner1"
android:layout_marginTop="1dp"
android:background="@android:color/holo_blue_dark"
android:text="button"
android:textColor="@android:color/white" />
</RelativeLayout>