Android 如何将文本对齐到任何要对齐的位置?
基本上,我希望我的布局是这样的: 必须有一个空格将imageView与文本隔开。中间的Ok按钮,但我认为主要的挑战是确保粗体文本与常规文本非常紧密地对齐(几乎接触到提示),并且位于其上方。此外,增加的挑战是,所有这些都必须使用线性布局来完成。到目前为止,我一直在使用垂直方向的线性布局,然后是水平方向的线性布局。水平方向线性布局包括图像视图和常规文本。我的主要问题是,我无法使粗体文本和常规文本按照我需要的方式对齐 顺便说一下,我去掉了所有的id和图像,因为它们都是私有的,但是添加它们应该没有问题Android 如何将文本对齐到任何要对齐的位置?,android,android-layout,Android,Android Layout,基本上,我希望我的布局是这样的: 必须有一个空格将imageView与文本隔开。中间的Ok按钮,但我认为主要的挑战是确保粗体文本与常规文本非常紧密地对齐(几乎接触到提示),并且位于其上方。此外,增加的挑战是,所有这些都必须使用线性布局来完成。到目前为止,我一直在使用垂直方向的线性布局,然后是水平方向的线性布局。水平方向线性布局包括图像视图和常规文本。我的主要问题是,我无法使粗体文本和常规文本按照我需要的方式对齐 顺便说一下,我去掉了所有的id和图像,因为它们都是私有的,但是添加它们应该没有问题
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="0dp"
android:text="Bold Text"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="13dp"
android:layout_marginTop="12dp"
android:src="@drawable/" />
<TextView
android:id="@+id/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Regular Text"
android:textColor="@color/black" />
</LinearLayout>
<Button
android:id="@+id/"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ok"
/>
</LinearLayout>
将此添加到TextView参数:
android:baselineAlignBottom="true"
父级应该是垂直线性布局,就像现在一样 第一个子项应该是水平线性布局 它的第一个子项应该是ImageView 第二个子项应为粗体文本,重心=水平 你父母的第二个孩子应该是普通的文本等等,重力=中心水平 父项的第三个子项应为“确定”按钮,重心=水平
如果你想在事情发生之前增加更多的空间,给他们安卓:margintart=“some_value”除非这是学校的项目或者我不会使用像这样的嵌套布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitXY"
app:srcCompat="@drawable/ic_launcher_background" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Bold Text"
android:textStyle="bold" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
编辑:
您不能拖放以实现您的愿望吗?为什么只使用线性布局?这样做是不对的。别再把时间浪费在毫无意义的限制上了。我知道,这是给我的要求@你不能在线性布局中拖放,但你可以在约束和相对布局中拖放。没问题。。。很高兴它能帮上忙如果我能问些别的问题,我怎样才能将图像居中放置在两个文本视图之间以及它所在的空间内?我不想使用设置为“dps”的marginLeft、marginRight等来“强行执行”。很抱歉,我无法想象你在问什么。是否仍希望图像位于两个文本视图的左侧,然后垂直居中?或者textview-Image-textview是水平的?或者完全不同的东西我希望imageView仍然在左边。我只想把它往下推一点。基本上是的,在两个文本视图的左边,然后垂直居中。我仍然不能100%确定我理解你的要求,但我在上面做了编辑。我假设图像很短(10dp),然后在图像属性中添加了android:layout\u gravity=“center\u vertical”。
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="10dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitXY"
android:layout_gravity="center_vertical"
app:srcCompat="@drawable/ic_launcher_background" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:padding="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/img"
android:layout_width="75dp"
android:layout_height="50dp"
android:layout_marginEnd="16dp"
android:src="@drawable/dummy_camera" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Bold Text"
android:gravity="bottom"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="20dp"
android:gravity="top"
android:text="Regular Text"
android:textColor="@color/black" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ok" />
</LinearLayout>