wb安卓屏幕适配

安卓屏幕适配

自定义一个MyRelativeLayout.java控件

package com.example.administrator.mywb;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

public class MyRelativeLayout  extends RelativeLayout {

    public MyRelativeLayout(Context context) {
        super(context);
    }

    //这个类不能删除的
    public MyRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        float scalex = UIUtils.getInstance(getContext()).getHorValue();//UI操作,取得x轴的缩放
        float scaley = UIUtils.getInstance(getContext()).getHorValue();//UI操作,取得y轴的缩放
        int count = this.getChildCount();
        for(int i = 0; i<count; i++){
            View child = this.getChildAt(i);
            LayoutParams layoutParams = (LayoutParams)child.getLayoutParams();
            layoutParams.width = (int)(layoutParams.width*scalex);
            layoutParams.height = (int)(layoutParams.height*scaley);
            layoutParams.leftMargin = (int)(layoutParams.leftMargin*scalex;
            layoutParams.rightMargin = (int)(layoutParams.rightMargin*scalex);
            layoutParams.topMargin = (int)(layoutParams.topMargin*scaley);
            layoutParams.bottomMargin = (int)(layoutParams.bottomMargin*scaley);
        }
    }

}


在XML中将LinearLayout格式修改为MyRelativeLayout.java控件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.MyRelativeLayout	//改这里
    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"
    >

    <TextView
        android:id="@+id/tv"
        android:layout_width="1040px"
        android:layout_height="80px"
       	android:layout_marginLeft="20px"
        android:layout_marginTop="50px"
        android:layout_marginRight="20px"
        android:backgroud="#f00"
        android:gravity="center" />

</android.support.constraint.MyRelativeLayout>	//改这里
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值