PercentRelativeLayoutからConstraintLayoutへの移行
Android Support Library26.0.0にて、Percent Support LibraryがDeprecatedになりました。
PercentRelativeLayoutなどを使用していたところは、ConstraintLayoutに置き換える必要があります。
特に何も考えずに、単純に置き換える方法を記載します。
基本的な使い方はこちらへ
ここでは、Layoutの真ん中に16:9のレイアウトを親レイアウトの横幅いっぱいに表示するサンプルを示します。
PercentRelativeLayoutでの実装
PercentRelativeLayoutを使用しての実装は以下です。
<?xml version="1.0" encoding="utf-8"?> <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="match_parent"> <FrameLayout android:id="@+id/imageView" android:layout_height="0dp" android:layout_width="0dp" android:layout_centerInParent="true" android:background="@color/colorAccent" app:layout_widthPercent="100%" app:layout_aspectRatio="178%" /> </android.support.percent.PercentRelativeLayout>
ConstraintLayoutでの実装
ConstraintLayoutを使って同じレイアウトを表現すると以下のようになります。
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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="match_parent"> <FrameLayout android:layout_width="0dp" android:layout_height="0dp" android:background="@color/colorAccent" app:layout_constraintDimensionRatio="H,16:9" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"/> </android.support.constraint.ConstraintLayout>
意外と簡単ですね。
以上です。
参考
PercentRelativeLayout | Android Developers
ConstraintLayout | Android Developers
android - How to use layout_aspectRatio in the PercentRelativeLayout? - Stack Overflow