将多个文本视图对齐到所有屏幕的中心和类似的左对齐

Align mutliple textview to both center and similar left alignment for all screens

我正在处理底部 sheet 对话框,想知道如何将所有 3 个文本视图居中对齐以及如何将它们与相同的左边距对齐。请注意,对于所有 3 行(这里的 3 行是重命名、共享和删除),文本视图还留有一个图像视图。 重心使它们对齐到中心,但它们的左边距不会相同。我希望他们的左边距也相同

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:id="@+id/btmsheet"
    android:background="@color/white"
    android:layout_marginLeft="@dimen/pad_5dp"
    android:layout_marginRight="@dimen/pad_5dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:id="@+id/dragView"
        android:layout_marginTop="@dimen/pad_10dp">

        <View
            android:layout_width="@dimen/pad_50dp"
            android:layout_height="@dimen/pad_5dp"
            android:background="@color/grey_color" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="15dp">

            <TextView
                android:id="@+id/bottom_sheet_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="@dimen/text_20sp"
                android:textColor="@color/black"
                android:text=""
                tools:text="RB000019.JPG"
                android:textStyle="bold"/>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="#E5E5E5" />

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:id="@+id/lyt_duplicate"
            android:orientation="horizontal"
            android:padding="15dp"
       >

            <TextView
                android:id="@+id/bottom_sheet_rename"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableStart="@drawable/edit"
                android:drawablePadding="@dimen/pad_15dp"
                android:paddingStart="@dimen/pad_20dp"
                android:text="@string/rename_project_name"
                android:textColor="@color/black"
                android:textSize="@dimen/text_16sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </android.support.constraint.ConstraintLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="#E5E5E5" />

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:id="@+id/lyt_rename"
            android:orientation="horizontal"
            android:padding="15dp"
            android:gravity="center_horizontal|center_vertical">

            <TextView
                android:id="@+id/bottom_sheet_share"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_16sp"
                android:textColor="@color/black"
                android:text="@string/share_project"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:drawableStart="@drawable/ic_share"
                android:drawablePadding="@dimen/pad_15dp"/>
        </android.support.constraint.ConstraintLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="#E5E5E5" />

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:orientation="horizontal"
            android:padding="15dp"
            android:id="@+id/lyt_move"
            android:gravity="center_horizontal|center_vertical">


            <TextView
                android:id="@+id/bottom_sheet_delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_16sp"
                android:textColor="@color/black"
                android:text="@string/delete_project"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:drawableStart="@drawable/delete"
                android:layout_marginStart="@dimen/pad_5dp"
                android:drawablePadding="@dimen/pad_20dp"
               />
        </android.support.constraint.ConstraintLayout>


    </LinearLayout>




</LinearLayout>

有多种方法可以实现上述目标。

  1. 您可以使用垂直 LinearLayout 并在内部有 3 个水平 LinearLayout,环绕图标和文本视图。
  2. 您也可以使用 ConstraintLayout - 这样布局树的深度就会更小。
  3. 您可以使用 RecyclerView,其中每个项目都是水平的 LinearLayout 或 ConstraintLayout - 代表行

根据与用户的聊天更新:

这是给大家一个思路:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.4" />

    <ImageView
        android:id="@+id/renameIcon"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_margin="16dp"
        android:background="#ff0000"
        app:layout_constraintEnd_toStartOf="@+id/guide"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/renameText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rename Project"
        app:layout_constraintBottom_toBottomOf="@+id/renameIcon"
        app:layout_constraintStart_toEndOf="@+id/guide"
        app:layout_constraintTop_toTopOf="@+id/renameIcon" />

    <ImageView
        android:id="@+id/shareProjectIcon"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_margin="16dp"
        android:background="#ff0000"
        app:layout_constraintEnd_toStartOf="@+id/guide"
        app:layout_constraintTop_toBottomOf="@+id/renameIcon" />

    <TextView
        android:id="@+id/shareProject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Share Project"
        app:layout_constraintBottom_toBottomOf="@+id/shareProjectIcon"
        app:layout_constraintStart_toEndOf="@+id/guide"
        app:layout_constraintTop_toTopOf="@+id/shareProjectIcon" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是它的样子:

这是一个想法,但仍然不是最优方案。

您可以做的是在现有的线性布局中创建另一个线性布局,并使其高度和宽度都达到 wrap_content。稍后使内部线性布局对齐到中心,最后保留特定数字的填充以对齐要对齐的文本。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:padding="4dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"/>

</LinearLayout>