还记得我们公司的app支持300多种国家的语言么。
还记得我们公司的测试妹子为了测试多语言导致现在还单身么。
帅气的我出现了,解放了程序员的适配时间,解放了妹子们去date的时间。
手写百分比控件布局,我觉得要比用谷歌官方的好一些。虽然我也没看谷歌官方是怎么写的。
这么帅气的我你怎么可能不给我点赞呢?
思路
给个百分数,乘以父控件的 宽高,得到的值 赋值给子view。就搞定了
抛砖引玉 来张图感受一下
- 第一个是什么都没做的
- 第二个是用了百分比布局的,并且用了我之前的写字体根据宽高自动缩放的控件 地址:http://blog.csdn.net/wanghao200906/article/details/50802875
- 第三个button也是用了百分比布局的。
效果就是这样。朴实无华,简单实用。 下面代码拿走不谢
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>