Android 如何将复杂的LinearLayout更改为ConstraintLayout

Android 如何将复杂的LinearLayout更改为ConstraintLayout,android,xml,android-linearlayout,android-constraintlayout,Android,Xml,Android Linearlayout,Android Constraintlayout,PercentRelativeLayout在API 26中被弃用,我在弃用之前创建了这个布局。有人知道在确保设计保持不变的同时,将复杂布局更改为约束布局的最佳方法是什么吗 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.c

PercentRelativeLayout在API 26中被弃用,我在弃用之前创建了这个布局。有人知道在确保设计保持不变的同时,将复杂布局更改为约束布局的最佳方法是什么吗

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:weightSum="100">
    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map_stationmap_darktheme"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_weight="90"
        android:layout_width="match_parent"
        android:layout_height="0dp" />
    <android.support.percent.PercentRelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp">
        <ImageView
            android:id="@+id/imageViewSun"
            app:layout_widthPercent="40%"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            app:srcCompat="@drawable/ic_sun_white" />

        <FrameLayout
            android:id="@+id/switch_tgl"
            app:layout_widthPercent="20%"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:layout_toEndOf="@id/imageViewSun">
            <android.support.v7.widget.SwitchCompat
                android:id="@+id/switch_stationmap_darktheme"
                android:layout_gravity="center_horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent" />
        </FrameLayout>

        <ImageView
            android:id="@+id/imageViewMoon"
            android:importantForAccessibility="no"
            app:layout_widthPercent="40%"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            app:srcCompat="@drawable/ic_moon_white"
            android:layout_toEndOf="@id/switch_tgl"/>
    </android.support.percent.PercentRelativeLayout>
</LinearLayout>


重点关注将
相对百分比LAyout
转换为
约束LAyout
,有两种方法。如果您是最近发布的
ConstraintLayout
的beta版,您可以使用两个新属性,这些属性似乎有助于百分比布局的日落:

    app:layout_constraintWidth_default="percent"
    app:layout_constraintWidth_percent="0.5"
关于此功能的在线文档并不多。是我遇到的一个

如果您不在beta版本中,您仍然可以通过使用指导原则并在这些指导原则之间约束您的视图来实现您想要的。 这是一个问题的答案,您可能会发现如何使用这两种技术很有用