Android 将线性布局转换为约束布局

Android 将线性布局转换为约束布局,android,xml,android-layout,android-linearlayout,android-constraintlayout,Android,Xml,Android Layout,Android Linearlayout,Android Constraintlayout,我正在学习使用ConstraintLayout,但我仍然无法习惯它。你能帮我把这个使用LinearLayout制作的布局“翻译”成ConstraintLayout,帮助我理解如何使用它来构建“稍微”复杂的布局吗?使用LinearLayout很简单,但使用约束构建这种布局似乎有问题 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/r

我正在学习使用ConstraintLayout,但我仍然无法习惯它。你能帮我把这个使用LinearLayout制作的布局“翻译”成ConstraintLayout,帮助我理解如何使用它来构建“稍微”复杂的布局吗?使用LinearLayout很简单,但使用约束构建这种布局似乎有问题

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.xxx.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="8dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:orientation="vertical">

        <EditText
            android:id="@+id/editText_main_overallTime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:inputType="time" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Exercised done"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/textView_main_exercisesDone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="18sp"
            tools:text="3/15" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:gravity="center"
            android:text="Next exercise"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/textView_main_nextExerciseName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:gravity="center"
            android:textSize="18sp"
            tools:text="Paradiddle" />
    </LinearLayout>

</LinearLayout>

<View
    android:id="@+id/view"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/colorPrimaryDark"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"/>

<TextView
    android:id="@+id/textView_main_currentExerciseName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="24sp"
    tools:text="Name of the excercise" />

<EditText
    android:id="@+id/editText_main_timeLeft"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="time" />

<ImageView
    android:id="@+id/imageView_main_exercise"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_weight="1"
    app:srcCompat="@mipmap/ic_launcher" />

<ImageButton
    android:id="@+id/imageButton_main_power"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    app:srcCompat="@android:drawable/ic_media_play" />

</LinearLayout>

使用Android Studio,您可以通过以下步骤实现所需的结果:

  • 打开设计选项卡->组件树->右键单击->将线性布局转换为约束布局
  • 选择适当的选项并单击OK

  • 选中文本选项卡,布局现在将转换为ConstraintLayout。

    将该XML放入布局资源中。在Android Studio中打开它。在“组件树”视图中的根
    LinearLayout
    上单击鼠标右键,然后从关联菜单中选择“将LinearLayout转换为ConstraintLayout”。Android Studio将尝试为您转换布局。此外,别忘了添加到您的鼓入门应用程序中。:-)