Android 将多页文本视图对齐到所有屏幕的中间和类似的左对齐
我正在处理一个底部工作表对话框,想知道如何将所有3个文本视图对齐到中心,以及如何将它们对齐到相同的左边距。请注意,对于所有3行,文本视图的左侧还有一个imageview(这里3行是重命名、共享和删除)。 重心使它们与中心对齐,但它们的左边距将不相同。我希望它们的左边距也一样Android 将多页文本视图对齐到所有屏幕的中间和类似的左对齐,android,uitextview,bottom-sheet,margins,Android,Uitextview,Bottom Sheet,Margins,我正在处理一个底部工作表对话框,想知道如何将所有3个文本视图对齐到中心,以及如何将它们对齐到相同的左边距。请注意,对于所有3行,文本视图的左侧还有一个imageview(这里3行是重命名、共享和删除)。 重心使它们与中心对齐,但它们的左边距将不相同。我希望它们的左边距也一样 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
<?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>
实现上述目标的方法有很多
<?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>
这里有一个想法,但它仍然不是最理想的解决方案
您可以做的是在现有线性布局内创建另一个线性布局,并使其在高度和宽度上都包含内容。之后,将内部线性布局与中心对齐,最后保留一个特定数字的填充以对齐要对齐的文本
<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>
我已经使用您的方法1开发了,文本正在与中心对齐,但并非所有3个TexView都有相同的起始边距如果我手动硬编码起始边距,它不会适用于所有设备。您需要在此处发布您的成果。有人可以选择你的编辑并调整它。这个问题供你参考谢谢@ror它现在对我有用。这三行之所以看起来像这样是因为ImageView
s位于它们的左侧,而您可能正在将整个视图(text+图标)居中。它们看起来只是没有居中,但它们是。如果您仅水平居中放置TextView
s,并将ImageView
s放在它们的左侧(XML内部),它应该可以完成此任务。我已添加了代码,请检查哦,我明白了。您通过drawableStart
在TextView
中创建它。尝试删除drawableStart
参数,而不是将ImageView
放在TextView
的左侧,以表示您的drawable,然后仅居中TextView
@SkypeDogg您所说的将使所有文本居中,但这三个文本视图是否都具有相同的左边距?我希望他们都有相同的左边距和一点中间请检查我的问题标题