2017-08-14 235 views
-1

PercentRelativeLayout自API 26起棄用,我在棄用前創建了此佈局。有誰知道什麼是將這個複雜的佈局改爲ConstraintLayout的最好方法,同時確保設計保持完全一樣?如何將複雜的LinearLayout更改爲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> 

enter image description here

回答

1

着眼於PercentRelativeLayoutConstraintLayout轉換,有幾個辦法。如果你是ConstraintLayout最近的測試版,您可以使用一對夫婦的這些措施似乎旨在幫助百分比佈局的棄用新的屬性:

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

沒有大量的文檔資料的在線使用此功能。 Here是我碰到的一個。

如果您尚未發佈測試版,則仍然可以通過使用準則並在這些準則之間約束您的視圖來完成您想要的操作。 Here是一個問題的答案,您可能會發現如何使用這兩種技術都很有用。