【问题标题】:How to change complex LinearLayout to ConstraintLayout如何将复杂的 LinearLayout 更改为 ConstraintLayout
【发布时间】:2017-08-14 13:26:14
【问题描述】:

PercentRelativeLayout 自 API 26 起已弃用,我在弃用之前创建了此布局。有谁知道将这种复杂布局更改为 ConstraintLayout 同时确保设计保持完全相同的最佳方法是什么?

<?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>

【问题讨论】:

    标签: android xml android-linearlayout android-constraintlayout


    【解决方案1】:

    关注PercentRelativeLayoutConstraintLayout 的转换,有几种方法。如果您是ConstraintLayout 的最新测试版,您可以使用一些似乎旨在帮助取消百分比布局的新属性:

        app:layout_constraintWidth_default="percent"
        app:layout_constraintWidth_percent="0.5"
    

    此功能的在线文档并不多。 Here 是我遇到的一个。

    如果您未使用 Beta 版,您仍然可以通过使用指南并将您的视图限制在这些指南之间来完成您想要的事情。 Here 是对您可能会发现对如何使用这两种技术有用的问题的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 2014-06-07
      • 1970-01-01
      • 2019-03-12
      相关资源
      最近更新 更多