【问题标题】:Android Studio layer below each other while orientation is horizontalAndroid Studio 层在彼此下方,而方向是水平的
【发布时间】:2020-05-18 17:52:58
【问题描述】:

做这个层工作,不知道如何把层放在我做的层下面,当android方向是水平的时候,它一直把我的层推到外面我有一张照片我做了什么以及它的外观,如果您有任何想法会很高兴听到,提前谢谢您。How it has to look
What I have done

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal">


        <TextView

            android:layout_width="140dp"
            android:layout_height="300dp"
            android:background="#FA0000" />


        <TextView
            android:layout_width="140dp"
            android:layout_height="300dp"
            android:background="#03ED0F" />

        <TextView
            android:id="@+id/blue"
            android:layout_width="140dp"
            android:layout_height="300dp"
            android:background="#0027C3"
            />

        <TextView
            android:layout_below="@id/blue"
            android:layout_width="100dp"
            android:layout_height="10dp"
            android:background="#000000" />




    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 我建议您使用权重总和来平均分配视图宽度 ...

标签: android xml android-studio layout


【解决方案1】:

如果您正在使用 你应该使用这些属性来控制位置

app:layout_constraintTop_toBottomOf
app:layout_constraintTop_toTopOf
app:layout_constraintTop_toStartOf
app:layout_constraintTop_toRightOf

根据它应该是什么样子,你可以试试

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
    android:id="@+id/ll_horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent">


    <TextView
        android:layout_width="140dp"
        android:layout_height="300dp"
        android:background="#FA0000" />


    <TextView
        android:layout_width="140dp"
        android:layout_height="300dp"
        android:background="#03ED0F" />

    <TextView
        android:layout_width="140dp"
        android:layout_height="300dp"
        android:background="#0027C3"
        />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="vertical"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@id/ll_horizontal"
    app:layout_constraintBottom_toBottomOf="parent">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#FA0000" />


    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#03ED0F" />

    <TextView
        android:id="@+id/blue"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#0027C3"
        />
</LinearLayout>

应该是这样的

【讨论】:

    【解决方案2】:

    在根目录下使用 2 个单独的 LinearLayout。 一个用于水平视图。其次是垂直视图。 并在 LinearLayout 中使用weight_sum 属性,以及在子视图中使用weight 属性来平等地分配子视图。

    【讨论】:

    • 更好的方法是使用相对布局将所有子视图保存在单个父布局中。
    • 谢谢,我也试试那个,需要做不同的变化。
    猜你喜欢
    • 2021-08-04
    • 2013-08-06
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    相关资源
    最近更新 更多