安卓适配问题

## 屏幕适配 ##

> 主流屏幕: 1280*720, 遵循原则: 不用AbsoluteLayout(绝对布局), 多用相对布局&线性布局(权重), 要用dp,不用px
>
> 开发后期, 在不同分辨率屏幕上测试(480*800,1920*1080), 如果没出现太大问题(影响正常使用), 就可以上线
>
> 如果后期测出问题怎么办?

- 图片适配

    不是很常用

- 布局适配

    不是很常用,  layout-800x480 专门适配480*800的屏幕

- 尺寸适配

    dp和px的关系: dp = px/设备密度

    float density = getResources().getDisplayMetrics().density;
    System.out.println("设备密度:" + density);
    320*240(0.75), 480*320(1),480*800(1.5),1280*720(2)

 1 public class DensityUtils {
 2 
 3     /**
 4      * dp转化px
 5      */
 6     public static int dp2px(Context ctx, float dp) {
 7         float density = ctx.getResources().getDisplayMetrics().density;
 8         int px = (int) (dp * density + 0.5f);// 4.9->5 4.4->4
 9 
10         return px;
11     }
12 
13          /**
14      * px转化dp
15      */
16     public static float px2dp(Context ctx, int px) {
17         float density = ctx.getResources().getDisplayMetrics().density;
18         float dp = px / density;
19 
20         return dp;
21     }
22 }

然后再调用这个工具类

 1 //初始化引导页的圆点
 2         for(int i=0;i<guideimage.length;i++){
 3             View point=new View(this);
 4             point.setBackgroundResource(R.drawable.point_grey);//设置引导页圆点颜色
 5             
 6             LinearLayout.LayoutParams params=new LayoutParams(DensityUtils.dp2px(this, 10), DensityUtils.dp2px(this, 10));
 7             if(i>0){
 8                 params.leftMargin=DensityUtils.dp2px(this, 10);//设置圆点间距
 9             }
10             point.setLayoutParams(params);//设置圆点大小
11             
12             ll_point_grey.addView(point);//将圆点放置LinnearLayout中
13         }

 



    values->dimens.xml 

1 <resources>
2 
3     <!-- Default screen margins, per the Android Design guidelines. -->
4     <dimen name="activity_horizontal_margin">16dp</dimen>
5     <dimen name="activity_vertical_margin">16dp</dimen>
6     
7     <dimen name="textview_width">160dp</dimen>
8 
9 </resources>

 

新建values-1280x720文件夹,创建dimens.xml文件中编写

 1 <resources> 2 3 <dimen name="textview_width">320dp</dimen> 4 5 </resources> 

 

然后在布局文件里使用

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:layout_width="match_parent"
 9         android:layout_height="100dp"
10         android:background="#f00" />
11 
12     <TextView
13         android:layout_width="@dimen/textview_width"
14         android:layout_height="100dp"
15         android:background="#0f0" />
16 
17 </LinearLayout>

这样的话,在用1280*720手机时就调用values-1280x720文件夹中dimens.xml的数据



- 权重适配

 1.   android:weightSum="3" 将宽度分成三等分,两个textview各占三分之一

      <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:orientation="horizontal" 16 android:weightSum="3" > 17 18 <TextView 19 android:layout_width="0dp" 20 android:layout_height="100dp" 21 android:layout_weight="1" 22 android:background="#0f0" /> 23 24 <TextView 25 android:layout_width="0dp" 26 android:layout_height="100dp" 27 android:layout_weight="1" 28 android:background="#00f" /> 29 </LinearLayout>

-代码适配

将宽度分成三等分,两个textview各占三分之一,用代码实现

 1 public class MainActivity extends Activity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_main);
 7 
 8         int width = getWindowManager().getDefaultDisplay().getWidth();
 9         int height = getWindowManager().getDefaultDisplay().getHeight();
10 
11         TextView tv1 = (TextView) findViewById(R.id.tv_1);
12         TextView tv2 = (TextView) findViewById(R.id.tv_2);
13 
14         LayoutParams params = new LayoutParams(width / 3, (int) (height * 0.2));
15 
16         tv1.setLayoutParams(params);
17         tv2.setLayoutParams(params);
18     }
19 
20 }

按比例适配

200/320为在320*480的模拟器中所占的比例大小,再乘以屏幕宽度,表示在任何模拟器上都已这比例展示
1 /**
2      * 
3      * 代码适配
4      */
5     int width = getWindowManager().getDefaultDisplay().getWidth();//获取屏幕宽度
6     slidingmenu.setBehindOffset(width*200/320);//设置主页面预留空间  200/320为在320*480的模拟器中所占的比例大小
7 //    slidingmenu.setBehindOffset(200);//设置主页面预留空间    

 

posted on 2016-03-15 20:20  oooo呼呼  阅读(262)  评论(0编辑  收藏  举报