自己手写百分比控件布局

还记得我们公司的app支持300多种国家的语言么。
还记得我们公司的测试妹子为了测试多语言导致现在还单身么。
帅气的我出现了,解放了程序员的适配时间,解放了妹子们去date的时间。

手写百分比控件布局,我觉得要比用谷歌官方的好一些。虽然我也没看谷歌官方是怎么写的。

这么帅气的我你怎么可能不给我点赞呢?

思路

给个百分数,乘以父控件的 宽高,得到的值 赋值给子view。就搞定了

抛砖引玉 来张图感受一下
这里写图片描述

效果就是这样。朴实无华,简单实用。 下面代码拿走不谢

package example.com.percent_layout;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

/**
 * Created by wanghao on 16/6/8.
 */
public class PercentRelativelayout extends RelativeLayout {
    private static final String TAG = "PercentRelativelayout";

    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 onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    //测量容器宽高
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        获取 viewgroup的宽高
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

//        测量出子空间的宽高进行比较

        int childCount = this.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = this.getChildAt(i);
            ViewGroup.LayoutParams layoutParams = child.getLayoutParams();
//            解析自定义的,进行替换
            float widthPercent = 0;
            float heightPercent = 0;
            if (layoutParams instanceof PercentRelativelayout.LayoutParams) {
                heightPercent = ((LayoutParams) layoutParams).getHeightPercent();
                widthPercent = ((LayoutParams) layoutParams).getWidthPercent();
            }
            Log.e(TAG, "onMeasure: "+widthPercent );
            if (widthPercent != 0) {
                layoutParams.width = (int) (width * widthPercent);
            }

            if (heightPercent != 0) {
                layoutParams.height = (int) (height * heightPercent);
            }
        }
        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;

        public float getWidthPercent() {
            return widthPercent;
        }

        public void setWidthPercent(float widthPercent) {
            this.widthPercent = widthPercent;
        }

        public float getHeightPercent() {
            return heightPercent;
        }

        public void setHeightPercent(float heightPercent) {
            this.heightPercent = heightPercent;
        }

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            TypedArray array = c.obtainStyledAttributes(attrs,
                    R.styleable.PercentRelativelayout);
            widthPercent = array.getFloat(R.styleable.PercentRelativelayout_layout_widthPercent, 0);
            heightPercent = array.getFloat(R.styleable.PercentRelativelayout_layout_heightPercent, 0);

        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(MarginLayoutParams source) {
            super(source);
        }
    }
}

xml文件

<?xml version="1.0" encoding="utf-8"?>
<example.com.percent_layout.PercentRelativelayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/percentRelativelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <example.com.percent_layout.stretchtextview
        android:id="@+id/stretch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:text="Hello World!Hello World!Hello World!"
        />

    <example.com.percent_layout.stretchtextview
        android:id="@+id/stretch2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/stretch"
        android:layout_marginTop="200dp"
        android:background="@color/colorAccent"
        android:text="Hello World!Hello World!Hello World!"
        app:layout_heightPercent="0.05"
        app:layout_widthPercent="0.3" />

    <Button
        app:layout_heightPercent="0.2"
        app:layout_widthPercent="0.3"
        android:text="button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/stretch2"
        />

</example.com.percent_layout.PercentRelativelayout>

源码在github上:https://github.com/wanghao200906/stretchtextview

http://blog.csdn.net/wanghao200906/article/details/51612613

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值