美文网首页
Android 屏幕适配方案解析(三)

Android 屏幕适配方案解析(三)

作者: 沪漂意哥哥 | 来源:发表于2022-06-01 10:07 被阅读0次

    百分比适配技术

    布局文件
    <?xml version="1.0" encoding="utf-8"?>
    <com.luisliuyi.percentlayout.demo.PercentRelativelayout 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">
    
       <TextView
           android:id="@+id/text"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Hello World!"
           android:background="#f00"
           android:gravity="center"
           app:widthPercent="0.5"
           app:heightPercent="0.5"
           app:marginLeftPercent="0.25"
           app:marginTopPercent="0.25"
           />
    
    </com.luisliuyi.percentlayout.demo.PercentRelativelayout>
    
    百分比ViewGroup
     /**
     * @author LiuYi
     * @date 2022/6/1 9:45 
     * @description:
     **/
    public class PercentRelativelayout extends RelativeLayout {
      public PercentRelativelayout(Context context) {
        super(context);
      }
    
      public PercentRelativelayout(Context context, AttributeSet attrs) {
        super(context, attrs);
      }
    
      public PercentRelativelayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
      }
    
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    
        for (int i = 0; i < getChildCount(); i++) {
          View child = getChildAt(i);
          if (child.getLayoutParams() instanceof  LayoutParams) {
            LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
            float widthPercent = layoutParams.widthPercent;
            float heightPercent = layoutParams.heightPercent;
            float marginLeftPercent = layoutParams.marginLeftPercent;
            float marginRightPercent = layoutParams.marginRightPercent;
            float marginTopPercent = layoutParams.marginTopPercent;
            float marginBottomPercent = layoutParams.marginBottomPercent;
    
            if (widthPercent > 0) {
              layoutParams.width = (int) (widthSize * widthPercent);
            }
            if (heightPercent > 0){
              layoutParams.height = (int) (heightSize * heightPercent);
            }
    
            if (marginLeftPercent > 0){
              layoutParams.leftMargin = (int) (widthSize * marginLeftPercent);
            }
    
            if (marginRightPercent > 0){
              layoutParams.rightMargin = (int) (widthSize * marginRightPercent);
            }
    
            if (marginTopPercent > 0){
              layoutParams.topMargin = (int) (heightSize * marginTopPercent);
            }
    
            if (marginBottomPercent > 0){
              layoutParams.bottomMargin = (int) (heightSize * marginBottomPercent);
            }
          }
          super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
      }
    
      @Override
      public RelativeLayout.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);
      }
    
      public static class LayoutParams extends RelativeLayout.LayoutParams {
        private float widthPercent;
        private float heightPercent;
        private float marginLeftPercent;
        private float marginRightPercent;
        private float marginTopPercent;
        private float marginBottomPercent;
        public LayoutParams(Context c, AttributeSet attrs) {
          super(c, attrs);
          TypedArray a = c.obtainStyledAttributes(attrs,R.styleable.PercentLayout);
          widthPercent = a.getFloat(R.styleable.PercentLayout_widthPercent, 0);
          heightPercent = a.getFloat(R.styleable.PercentLayout_heightPercent, 0);
          marginLeftPercent = a.getFloat(R.styleable.PercentLayout_marginLeftPercent, 0);
          marginRightPercent = a.getFloat(R.styleable.PercentLayout_marginRightPercent, 0);
          marginTopPercent = a.getFloat(R.styleable.PercentLayout_marginTopPercent, 0);
          marginBottomPercent = a.getFloat(R.styleable.PercentLayout_marginBottomPercent, 0);
          a.recycle();
        }
    
        public LayoutParams(int w, int h) {
          super(w, h);
        }
      }
    }
    
    定义attr
     <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="PercentLayout">
        <attr name="widthPercent" format="float" />
        <attr name="heightPercent" format="float" />
        <attr name="marginLeftPercent" format="float" />
        <attr name="marginRightPercent" format="float" />
        <attr name="marginTopPercent" format="float" />
        <attr name="marginBottomPercent" format="float" />
        </declare-styleable>
    </resources>
    
    
    显示效果
    image.png

    第三方库

     https://github.com/hongyangAndroid/android-percent-support-extend
    
     https://github.com/SenhLinsh/LshPercentLayout
    

    相关文章

      网友评论

          本文标题:Android 屏幕适配方案解析(三)

          本文链接:https://www.haomeiwen.com/subject/wtktmrtx.html