如何在 LinnearLayouts 中将两个彼此相邻的 TextViews 对齐?

我试图将两个 TextViews 放在一起,但不知何故我无法让它工作。我想要以下内容:txtViewAddress: txtAddress 我试过把 android:layout_toRightOf="@id/txtViewAddress" 我也试过把 layout_toLeftOf,但两者都不起作用。


希望大家能帮帮我。提前致谢。


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >


<TextView

    android:id="@+id/CalendarID"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text=""

    android:textAppearance="?android:attr/textAppearanceMedium"

    android:visibility="gone" />


<LinearLayout

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:orientation="horizontal">


    <LinearLayout

        android:layout_width="480dp"

        android:layout_height="wrap_content"

        android:orientation="vertical">



        <TextView

            android:id="@+id/txtViewAddress"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Address: "

            android:textAppearance="?android:attr/textAppearanceMedium"

            android:textSize="22dp"

            android:textStyle="bold"/>


        <TextView

            android:id="@+id/txtAddress"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_toEndOf="@id/txtViewAddress"

            android:text="Hello"

            android:textAppearance="?android:attr/textAppearanceMedium"

            android:textSize="22dp"

            android:textStyle="bold"/>

    </LinearLayout>



12345678_0001
浏览 199回答 3
3回答

MMMHUHU

如果您需要在线性布局中并排放置视图,则布局的方向必须是水平的。您还可以使用约束布局,它在将视图放置在屏幕上时具有更大的灵活性。

慕妹3146593

<LinearLayout&nbsp; &nbsp; android:layout_width="480dp"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:orientation="horizontal"&nbsp; &nbsp; android:weightSum="2">&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/txtViewAddress"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:text="Address: "&nbsp; &nbsp; &nbsp; &nbsp; android:layout_weight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:textAppearance="?android:attr/textAppearanceMedium"&nbsp; &nbsp; &nbsp; &nbsp; android:textSize="22dp"&nbsp; &nbsp; &nbsp; &nbsp; android:textStyle="bold"/>&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/txtAddress"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_toEndOf="@id/txtViewAddress"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_weight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:text="Hello"&nbsp; &nbsp; &nbsp; &nbsp; android:textAppearance="?android:attr/textAppearanceMedium"&nbsp; &nbsp; &nbsp; &nbsp; android:textSize="22dp"&nbsp; &nbsp; &nbsp; &nbsp; android:textStyle="bold"/></LinearLayout>更改android:orientation="vertical"为android:orientation="horizontal",您也可以使用android:weightSum="2" weightSum 来定义视图的优先级,如果您为线性布局中的所有视图赋予相同的权重,那么所有视图在线性布局中占据相同的空间

幕布斯6054654

这应该如下<LinearLayout&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="480dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:orientation="horizontal">................&nbsp; &nbsp; </LinearLayout>而且我建议不要为布局提供固定宽度,而是将其设置为屏幕大小
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java