将LinearLayout转换为ConstraintLayout

时间:2018-05-28 23:17:34

标签: android xml android-layout android-linearlayout android-constraintlayout

我正在学习使用ConstraintLayout,但我仍然无法习惯它。你能帮我翻译一下"翻译"这个布局使用LinearLayout进入ConstraintLayout来帮助我理解" bit"可以使用它构建更复杂的布局?使用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>

1 个答案:

答案 0 :(得分:0)

您可以使用Android Studio通过以下步骤获得所需的结果:

  1. 打开设计标签 - &gt;组件树 - &gt;右键单击 - &gt;将线性布局转换为约束布局
  2. convert layout

    1. 选择适当的选项,然后单击“确定” options
    2. 选中文本选项卡,您的布局现在将转换为ConstraintLayout。