【问题标题】:Alignment in LinearLayout on Android StudioAndroid Studio 上 LinearLayout 中的对齐
【发布时间】:2021-03-01 21:54:25
【问题描述】:

我是 Android Studio 的初学者,现在我被困住了。我环顾四周并搜索,但我找到的解决方案并没有解决我的问题或带来了另一个问题。
我的布局是这样的: view image

海拔:
10.0
纬度:
纬度测试
经度:
经度测试:

我需要在左边放置海拔、纬度和经度,在右边放置 10.0、latitudeTest 和 longitudeTeste,有点像这样:

海拔:                          10.0
纬度:              latitudeTest
经度:        longitudeTest

这是我的代码

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/item_horizontal_margin"
            android:layout_marginEnd="@dimen/item_horizontal_margin"
            android:orientation="vertical">

            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                <!--Altitude:-->
                android:text="@string/lb_altitude" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                <!--10.0:-->
                android:text="@={``+waypoint.altitude}" />

            <TextView
                android:id="@+id/lb_latitude"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                <!--Latitude:-->
                android:text="@string/lb_latitude" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/lb_latitude"
                android:text="latitudeTest" />

            <TextView
                android:id="@+id/lb_longitude"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                <!--Longitude:-->
                android:text="@string/lb_longitude" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/lb_longitude"
                android:text="longitudeTeste" />
        </LinearLayout>

感谢您的帮助!

【问题讨论】:

    标签: android android-studio layout alignment


    【解决方案1】:

    以下应该有效。您需要将 TextViews 放置在不同的布局中以排列它们的位置。你会习惯的,一开始很复杂。

    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/item_horizontal_margin"
                android:layout_marginEnd="@dimen/item_horizontal_margin"
                android:orientation="vertical">
    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    <!--Altitude:-->
                    android:text="@string/lb_altitude" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    <!--10.0:-->
                    android:text="@={``+waypoint.altitude}" />
    </LinearLayout>
    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:id="@+id/lb_latitude"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    <!--Latitude:-->
                    android:text="@string/lb_latitude" />
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/lb_latitude"
                    android:text="latitudeTest" />
    </LinearLayout>
    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:id="@+id/lb_longitude"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    <!--Longitude:-->
                    android:text="@string/lb_longitude" />
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/lb_longitude"
                    android:text="longitudeTeste" />
    </LinearLayout>
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      LinearLayout “一种将其他视图水平排列在单列中或垂直排列在单行中的布局。”来自here

      在 LinearLayout 中,您只有一列。如果您想让多个元素在两个方向上彼此相邻,请使用另一个布局。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-19
        • 1970-01-01
        • 2013-10-10
        • 1970-01-01
        • 2018-10-12
        • 2018-09-25
        相关资源
        最近更新 更多