您的位置:首页 > 移动开发 > Android开发

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

2016-10-26 13:38 555 查看
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的值为子控件权重之和

[b]5、代码适配[/b]

 

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转为像素之后再设置
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: