zoukankan      html  css  js  c++  java
  • 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转为像素之后再设置

  • 相关阅读:
    关于大文本(txt)导入sqlserver2008数据库的一点感想
    欲则不达
    记录下马上两年的大学吧
    osgi使用 equnix框架 Bridge方式搭建方法(部分来源于ibm网站)
    mavan deploy不出repository.xml
    在项目中使用maven私服
    playbook 若干问题
    Maven 3 Felix 4 Eclipse 的搭建与部署(部分转载自别人文章)
    wallwd
    很好的面试记录
  • 原文地址:https://www.cnblogs.com/ecollab/p/6064694.html
Copyright © 2011-2022 走看看