安卓百分比布局的使用

本文详细介绍了在Android开发中如何使用百分比布局,通过实例展示了如何实现屏幕适配,让UI元素根据屏幕尺寸按比例调整大小,提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安卓百分比布局的使用: 在gradle中加此行代码 compile 'com.zhy:percent-support-extends:1.0.1' 
他有PercentRelativeLayout、PercentFrameLayout两个类,
我们可以根据他们延伸出其他布局 比如线性布局(LinearL
 
package com.example.mrzhang.myapplication.MyControl;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.percent.PercentLayoutHelper;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**
 * Created by MrZhang on 15/6/30.
 * 自定义组件  实现线性百分比布局
 */
public class PercentLinearLayout extends LinearLayout
{

    private PercentLayoutHelper mPercentLayoutHelper;

    public PercentLinearLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        mPercentLayoutHelper = new PercentLayoutHelper(this);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        mPercentLayoutHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mPercentLayoutHelper.handleMeasuredStateTooSmall())
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        super.onLayout(changed, l, t, r, b);
        mPercentLayoutHelper.restoreOriginalParams();
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs)
    {
        return new LayoutParams(getContext(), attrs);
    }


    public static class LayoutParams extends LinearLayout.LayoutParams
            implements PercentLayoutHelper.PercentLayoutParams
    {
        private PercentLayoutHelper.PercentLayoutInfo mPercentLayoutInfo;

        public LayoutParams(Context c, AttributeSet attrs)
        {
            super(c, attrs);
            mPercentLayoutInfo = PercentLayoutHelper.getPercentLayoutInfo(c, attrs);
        }

        @Override
        public PercentLayoutHelper.PercentLayoutInfo getPercentLayoutInfo()
        {
            return mPercentLayoutInfo;
        }

        @Override
        protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr)
        {
            PercentLayoutHelper.fetchWidthAndHeight(this, a, widthAttr, heightAttr);
        }
        //可加也可以不加
//        public LayoutParams(int width, int height) {
//            super(width, height);
//        }
//
//
//        public LayoutParams(ViewGroup.LayoutParams source) {
//            super(source);
//        }
//
//        public LayoutParams(MarginLayoutParams source) {
//            super(source);
//        }

    }

}
 
<?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="match_parent">

    <com.example.mrzhang.myapplication.MyControl.PercentLinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:gravity="center"
            android:id="@+id/text"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#000000"
            android:text="textview"
            android:textColor="#ffffff"
            app:layout_heightPercent="10%"
            app:layout_widthPercent="100%" />

        <TextView
            android:id="@+id/tiaozuan"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#ff0000"
            android:text="textview"
            android:textColor="#ffffff"
            app:layout_heightPercent="10%"
            app:layout_widthPercent="100%" />


        <Button
            android:gravity="center"
            android:id="@+id/xiangce"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="相册"
            app:layout_heightPercent="10%"
            app:layout_widthPercent="100%" />
        <ImageView
            android:id="@+id/imageview"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_heightPercent="30%"
            android:layout_gravity="center"
            app:layout_widthPercent="50%"/>

        <com.example.mrzhang.myapplication.MyAsynchronization.NumberKeyboardView
            android:id="@+id/numbers"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:gravity="center"
            android:layout_gravity="center_horizontal"
            app:layout_heightPercent="40%"
            app:layout_widthPercent="100%" />


    </com.example.mrzhang.myapplication.MyControl.PercentLinearLayout>


</LinearLayout>

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值