android百分比布局适配,安卓屏幕适配-百分比布局
屏幕適配-百分比布局
原理:以父容器尺寸作為參考,在View的加載過(guò)程,根據(jù)當(dāng)前父容器實(shí)際尺寸換算出目標(biāo)尺寸,在作用在View上。
百分比布局實(shí)際是對(duì)容器的一中擴(kuò)展,擴(kuò)展的是寬高等比例的設(shè)置。
1、自定義屬性
attrs.xml
2、對(duì)屬性進(jìn)行解析
以RelativeLayout為例,看一下它是如何對(duì)屬性進(jìn)行解析的?
RelativeLayout的工作原理:在它里面定義了一個(gè)LayoutParams靜態(tài)內(nèi)部類,在這里面定義了我們的自定義屬性,這些屬性都是我們這個(gè)容器的特有的屬性。
解析是在 public LayoutParams(Context c, AttributeSet attrs)構(gòu)造方法中:
現(xiàn)在我們看一下,這些屬性是如何添加到我們的View中的。
我們都知道PhoneWindow是Window的唯一實(shí)現(xiàn)類,我們從PhoneWindow的setContentView開(kāi)始
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");
final Context inflaterContext = mContext;
final AttributeSet attrs = Xml.asAttributeSet(parser);
Context lastContext = (Context) mConstructorArgs[0];
mConstructorArgs[0] = inflaterContext;
View result = root;
........
if (TAG_MERGE.equals(name)) {
if (root == null || !attachToRoot) {
throw new InflateException(" can be used only with a valid "
+ "ViewGroup root and attachToRoot=true");
}
rInflate(parser, root, inflaterContext, attrs, false);
} else {
// Temp is the root view that was found in the xml 表示我們布局文件的根布局
final View temp = createViewFromTag(root, name, inflaterContext, attrs);
ViewGroup.LayoutParams params = null;
if (root != null) {
if (DEBUG) {
System.out.println("Creating params from root: " +
root);
}
// Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params);
}
}
...........
}
我們看到重要的核心代碼 params = root.generateLayoutParams(attrs);和
temp.setLayoutParams(params);
如下就是我們自定義的百分不布局:
public class PercentLayout extends RelativeLayout {
public PercentLayout(Context context) {
super(context);
}
public PercentLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PercentLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//獲取父容器的尺寸
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);//重新設(shè)置子View的布局屬性,在進(jìn)行View的測(cè)量
ViewGroup.LayoutParams params = child.getLayoutParams();
//如果說(shuō)是百分比布局屬性
if (checkLayoutParams(params)){
LayoutParams lp = (LayoutParams)params;
//自定百分比屬性
float widthPercent = lp.widthPercent;
float heightPercent = lp.heightPercent;
float marginLeftPercent = lp.marginLeftPercent;
float marginRightPercent= lp.marginRightPercent;
float marginTopPercent= lp.marginTopPercent;
float marginBottomPercent = lp.marginBottomPercent;
//根據(jù)當(dāng)前父容器實(shí)際尺寸換算出目標(biāo)尺寸
if (widthPercent > 0){
params.width = (int) (widthSize * widthPercent);
}
if (heightPercent > 0){
params.height = (int) (heightSize * heightPercent);
}
if (marginLeftPercent > 0){
((LayoutParams) params).leftMargin = (int) (widthSize * marginLeftPercent);
}
if (marginRightPercent > 0){
((LayoutParams) params).rightMargin = (int) (widthSize * marginRightPercent);
}
if (marginTopPercent > 0){
((LayoutParams) params).topMargin = (int) (heightSize * marginTopPercent);
}
if (marginBottomPercent > 0){
((LayoutParams) params).bottomMargin = (int) (heightSize * marginBottomPercent);
}
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof LayoutParams;
}
public 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();
}
}
}
總結(jié)
以上是生活随笔為你收集整理的android百分比布局适配,安卓屏幕适配-百分比布局的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 在c语言中怎么修改信息,车管所信息变更
- 下一篇: android 手机内存其他文件夹里,手