1. 程式人生 > >Android 螢幕自適應(百分比)

Android 螢幕自適應(百分比)

新增依賴:

implementation 'com.android.support:percent:27.1.1'

接下來就看佈局了:

跟佈局:

控制元件:

紅框框中的佈局位置是必須有的

注意在設定寬高的時候是app

並且在後面一定要加Percent

設定佔位百分比

全部佈局程式碼:

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/one"
        android:text="one"
        android:background="@color/colorAccent"
        android:layout_gravity="left|top"
        app:layout_widthPercent="20%"
        app:layout_heightPercent="100%" />

    <Button
        android:id="@+id/two"
        android:text="two"
        android:background="@color/colorPrimaryDark"
        android:layout_gravity="top|right"
        app:layout_widthPercent="80%"
        app:layout_heightPercent="50%"
        />

    <Button
        android:id="@+id/three"
        android:text="three"
        android:background="@android:color/holo_purple"
        android:layout_gravity="bottom|right"
        app:layout_widthPercent="80%"
        app:layout_heightPercent="50%"
        />

</android.support.percent.PercentFrameLayout>

 效果: