android 项目学习随笔二十(屏幕适配)

1、图片适配

放入相同名称的资源文件,机器根据不同分辨率找相近的资源

240*320 ldpi
320*480 mdpi
480*800 hdpi
720*1280 xhdpi

2、布局适配

在不同的分辨率下显示不同的布局,定义不同分辨率的布局文件,一般控件相同(否则FindViewByID找不到) 

layout-800x480,  适配480*800分辨率的布局

3、尺寸适配

dp 设备独立像素

dp = px / 设备密度

float density = getResources().getDisplayMetrics().density;
System.out.println("设备密度:" + density);

分辨率   设备密度   像素(PX)

240*320 0.75 120px
320*480 1.0 160px
480*800 1.5 240px
1280*720 2 320px (主流屏幕)

1920*1080

设置为DP后,系统运行起来后会自动计算出像素

/res/values/dimens.xml

<resources>

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="textview_width">160dp</dimen>

</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#f00" />

    <TextView
        android:layout_width="@dimen/textview_width"
        android:layout_height="50dp"
        android:background="#0f0" />

</LinearLayout>

定义不同分辨率的values

import android.content.Context;

public class DensityUtils {

    public static int dp2px(float dp, Context ctx) {
        float density = ctx.getResources().getDisplayMetrics().density;
        // 4.1->4, 4.9->4
        int px = (int) (dp * density + 0.5f);// 加0.5可以四舍五入
        return px;
    }

    public static float px2dp(int px, Context ctx) {
        float density = ctx.getResources().getDisplayMetrics().density;
        float dp = px / density;
        return dp;
    }
}

在代码中指定宽度长度要进行dp和PX转化,代码所赋的值是像素(px)

4、权重适配

 只适合线性布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="3" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="#f00" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="#0f0" />
    </LinearLayout>

</LinearLayout>

android:layout_width="0dp"

把水平宽度等分为3份,两个TextView各占三分之一的宽度

如果weightSum不设置,weightSum的值为子控件权重之和

5、代码适配

 

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        float density = getResources().getDisplayMetrics().density;
        System.out.println("设备密度:" + density);

        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();

        TextView tvText = (TextView) findViewById(R.id.tv_text);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                width / 3, height / 10);
        tvText.setLayoutParams(params);
    }

 

总结:多用相对布局和线性布局(权重), 用dp不用px, 用sp不用px, 代码中如果必须写像素的话, 将dp转为像素之后再设置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值