在 RelativeLayout 中居中文本和图像
center text and image in RelativeLayout
我带着一个看似简单的问题来找你。但是我找不到合适的解决方案。
我在 LinearLayout 内的 RelativeLayout 内有一个 Image- 和一个 TextView。
<LinearLayout
android:id="@+id/page2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="horizontal"
android:weightSum="2"
android:baselineAligned="false"
android:divider="@drawable/separator_vertical"
android:showDividers="middle">
<RelativeLayout
android:id="@+id/delete_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="@+id/delete_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="DemoText"
android:textColor="#fff"
android:textSize="15sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@id/delete_text"
android:src="@drawable/ic_delete" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/edit_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="@+id/edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="DemoText"
android:textColor="#fff"
android:textSize="15sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@id/edit_text"
android:src="@drawable/ic_edit" />
</RelativeLayout>
</LinearLayout>
目前只有TextView在各自的RelativeLayout中居中。
如何在 RelativeLayout 的中心对齐 ImageView 和 TextView?
您可以将两个视图包裹在另一个布局中并将其居中。例如:
<RelativeLayout
android:id="@+id/delete_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="10dp"
android:src="@drawable/ic_delete" />
<TextView
android:id="@+id/delete_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DemoText"
android:textColor="#fff"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
设置
android:gravity="center"
到具有权重的父相对布局。
以便它按照您的意愿将其子项居中!!
LinearLayout有不同的特点,你需要的是ConstraintLayout和LinearLayout里面。当然你可以用其他东西包裹 RelativeLayout 但这是不必要的嵌套:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/page2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:baselineAligned="false"
android:orientation="horizontal"
android:showDividers="middle"
android:weightSum="2">
<LinearLayout
android:orientation="horizontal"
android:id="@+id/delete_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/guideline">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@id/delete_text"
android:src="@drawable/delete_text" />
<TextView
android:id="@+id/delete_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DemoText"
android:textColor="#fff"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:id="@+id/edit_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/guideline">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@id/edit_text"
android:src="@drawable/edit_text" />
<TextView
android:id="@+id/edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DemoText"
android:textColor="#fff"
android:textSize="15sp" />
</LinearLayout>
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
</android.support.constraint.ConstraintLayout>
我带着一个看似简单的问题来找你。但是我找不到合适的解决方案。 我在 LinearLayout 内的 RelativeLayout 内有一个 Image- 和一个 TextView。
<LinearLayout
android:id="@+id/page2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="horizontal"
android:weightSum="2"
android:baselineAligned="false"
android:divider="@drawable/separator_vertical"
android:showDividers="middle">
<RelativeLayout
android:id="@+id/delete_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="@+id/delete_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="DemoText"
android:textColor="#fff"
android:textSize="15sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@id/delete_text"
android:src="@drawable/ic_delete" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/edit_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="@+id/edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="DemoText"
android:textColor="#fff"
android:textSize="15sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@id/edit_text"
android:src="@drawable/ic_edit" />
</RelativeLayout>
</LinearLayout>
目前只有TextView在各自的RelativeLayout中居中。 如何在 RelativeLayout 的中心对齐 ImageView 和 TextView?
您可以将两个视图包裹在另一个布局中并将其居中。例如:
<RelativeLayout
android:id="@+id/delete_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="10dp"
android:src="@drawable/ic_delete" />
<TextView
android:id="@+id/delete_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DemoText"
android:textColor="#fff"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
设置
android:gravity="center"
到具有权重的父相对布局。
以便它按照您的意愿将其子项居中!!
LinearLayout有不同的特点,你需要的是ConstraintLayout和LinearLayout里面。当然你可以用其他东西包裹 RelativeLayout 但这是不必要的嵌套:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/page2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:baselineAligned="false"
android:orientation="horizontal"
android:showDividers="middle"
android:weightSum="2">
<LinearLayout
android:orientation="horizontal"
android:id="@+id/delete_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/guideline">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@id/delete_text"
android:src="@drawable/delete_text" />
<TextView
android:id="@+id/delete_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DemoText"
android:textColor="#fff"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:id="@+id/edit_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/guideline">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@id/edit_text"
android:src="@drawable/edit_text" />
<TextView
android:id="@+id/edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DemoText"
android:textColor="#fff"
android:textSize="15sp" />
</LinearLayout>
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
</android.support.constraint.ConstraintLayout>