【问题标题】:Align mutliple textview to both center and similar left alignment for all screens将多个文本视图对齐到所有屏幕的中心和类似的左对齐
【发布时间】:2020-06-02 09:55:17
【问题描述】:

我正在处理底部工作表对话框,想知道如何将所有 3 个文本视图对齐到中心以及将它们与相同的左边距对齐。请注意,所有 3 行的 textview 还留有一个 imageview(这里 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>

【问题讨论】:

  • 这 3 行看起来像这样,因为 ImageViews 在它们的左侧,您可能将整个 View 居中(文本 + 图标)。它们只是看起来像不以中心为中心,但实际上是。如果您仅将TextViews 水平居中并将ImageViews 放在它们的左侧(在XML 中),它应该可以完成这项工作。
  • 我已添加代码请检查
  • 哦,我明白了。您正在通过drawableStartTextView 中实现它。尝试删除drawableStart 参数,而不是将ImageView 放在TextView 的左侧以表示您的drawable,然后仅居中TextView
  • @SkypeDogg 您所说的将使所有文本居中,但是所有这 3 个文本视图的左边距是否相同?我希望它们都具有相同的左边距和一点中心
  • 请检查我的问题标题

标签: android uitextview bottom-sheet margins


【解决方案1】:

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

  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>

这是它的外观:

【讨论】:

  • 我已经使用你的方法 1 进行了开发,文本与中心对齐,但并非所有 3 个 texview 都具有相同的起始边距
  • 如果我手动硬编码 startmargins 它将不适用于所有设备
  • 你需要在这里发布你的努力。有人可以挑选您的编辑并进行调整。
  • 谢谢@ror,它现在对我有用。指南是一个救星
【解决方案2】:

这是一个想法,但仍然不是最佳解决方案。

您可以做的是在现有的线性布局中创建另一个线性布局,并使其高度和宽度都为 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>

【讨论】:

    猜你喜欢
    • 2020-05-14
    • 2020-05-13
    • 2021-09-20
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    相关资源
    最近更新 更多