屏幕适配和百分比布局
来源:互联网 发布:淘宝锂电池靠谱吗 编辑:程序博客网 时间:2024/05/17 02:35
导包 compile 'com.android.support:percent:23.3.0'
在res文件下创建文件 它会自动根据你安装的手机屏幕进行选择适配
文件的名字可以随便起
layout 普通的单面版的屏幕
layout-large 屏幕尺寸大于7英寸,同时系统版本在3.2之下
layout-sw600dp
layout-land 横屏
layout(layout-port) 竖屏
layout-sw600dp-land
layout-sw600dp-port
2.导包写代码 compile 'com.android.support:percent:23.3.0'
这里包括了两种布局: PercentRelativeLayout、PercentFrameLayout
支持的属性有:
layout_widthPercent、layout_heightPercent、
layout_marginPercent、layout_marginLeftPercent、
layout_marginTopPercent、layout_marginRightPercent、
layout_marginBottomPercent、layout_marginStartPercent、layout_marginEndPercent。
3.主布局
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/one_tv" android:layout_height="0dp" android:layout_width="0dp" android:text="w-40%,h-20%" android:gravity="center" android:layout_alignParentTop="true" app:layout_heightPercent="20%" app:layout_widthPercent="40%" android:background="#44ff0000"/> <TextView android:id="@+id/two_tv" android:layout_height="0dp" android:layout_width="0dp" android:gravity="center" android:text="w-30%,h-10%" android:background="#440000ff" app:layout_heightPercent="10%" app:layout_widthPercent="30%" app:layout_marginPercent="10%" android:layout_alignParentRight="true"/></android.support.percent.PercentRelativeLayout>
4.当然在当使用图片时,无法设置宽高的比例比如我们的图片宽高是200*100的,我们在使用过程中我们设置宽高为20%、10%,这样会造成图片的比例失调。
为什么呢?因为20%参考的是屏幕的宽度,而10%参考的是屏幕的高度。很难使用百分比定义一个正方形的控件
比如,我现在界面的右下角有一个FloatingActionButton,我希望其宽度和高度都为屏幕宽度的10%,很难做到。
所以可以写下面的代码
<com.zhy.android.percent.support.PercentLinearLayout xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="0dp" android:layout_height="0dp" android:gravity="center" android:text="width-30%,height-20%" app:layout_heightPercent="20%" app:layout_widthPercent="30%" android:background="@color/colorAccent"/> <TextView android:layout_width="0dp" android:layout_height="0dp" android:gravity="center" android:text="width-40%,height-20%" app:layout_heightPercent="20%w" app:layout_widthPercent="40%" android:background="@color/colorPrimary"/></com.zhy.android.percent.support.PercentLinearLayout>
5.主函数获取宽高和版本号
public class MainActivity extends AppCompatActivity { private WindowManager wm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main1); }// 获取屏幕的宽度和高度的方法 public void getWidthAndHeight(){ wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); int height = wm.getDefaultDisplay().getHeight(); int width = wm.getDefaultDisplay().getWidth(); Log.i("tag","height==="+height+",width==="+width); }// 获取屏幕宽度和高度的第二种方法 public void getScreenWAndH(){ wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); int widthPixels = outMetrics.widthPixels; int heightPixels = outMetrics.heightPixels; Log.i("tag","height==="+heightPixels+",width==="+widthPixels); }// 获取包名和版本号的方法 public void getPackageInfo(){ String packageName = this.getPackageName(); Log.i("TAG","包名==="+packageName);// 获取应用包管理器 PackageManager pm = this.getPackageManager(); try { PackageInfo info = pm.getPackageInfo(packageName, 0); int versionCode = info.versionCode; String versionName = info.versionName; Log.i("TAG","code==="+versionCode+",name==="+versionName); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } }}/** * 版本迭代的过程: * 打开app---》如果程序更新了---》会跳出对话框,提示更新。用户可以选择更新或者不更新。 * * 程序员的逻辑: * 1.在首页面,开发时,服务器会自动提供一个控制版本更新的接口url。 * 2.每一次用户打开app,都会自动连接这个网址,然后这个返回json数据,{ret:1,status:'连接成功了',versioncode:2,url:"http://......"} * 3.解析网址,拿出网址当中的版本号,和本地app的版本号进行对比,如果最新的版本和本地的不一致,就下载url对应的apk * 4.通常下载显示在通知栏,而且有进度条,下载成功之后,提示用户安装新版本。
6.你也可以添加一个图片加工代码
图片的加工处理代码://1.得到用来设置图片尺寸的参数的对象BitmapFactory.Options options = new BitmapFactory.Options();//2.解码边缘options.inJustDecodeBounds = true;//3.对图片进行解码BitmapFactory.decodeByteArray(data, 0, data.length, options);//4.获取图片原来的宽度和高度int oldWidth = options.outWidth;int oldHeight = options.outHeight;//5.得到压缩的比例double scaleWidth = oldWidth/newWidth; //宽度要压缩的比例double scaleHeight = oldHeight/newHeight; //高度要压缩的比例。//6.取出宽高的压缩比例当中较大的值作为缩放比例int scale = (int) Math.round(scaleHeight>scaleWidth?scaleHeight:scaleWidth);//7.设置参数当中的缩放比例,必须要设置大于1的整数,数越大,缩放越小options.inSampleSize = scale;//8.缩放边缘options.inJustDecodeBounds = false;//9.通过属性参数对象得到新的位图Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length,options);
- 屏幕适配和百分比布局
- Android屏幕适配,百分比布局
- Android屏幕适配,百分比布局
- Android百分比布局解决屏幕适配问题
- Android 控件屏幕适配之百分比布局
- 百分比布局的延伸,百分比布局自动适配,解决了安卓做屏幕适配的麻烦。
- Android:屏幕适配(2),Google百分比布局的扩展
- (屏幕适配)鸿洋大神的Google百分比布局库 android-percent-support-extend
- 安卓百分比布局---很好的解决屏幕适配问题
- 使用databinding,实现简单全面的百分比布局(屏幕适配的大招)
- Android 屏幕适配方案(二)百分比布局
- Android 屏幕适配方案(三)增强百分比布局
- android屏幕适配方案二(百分比布局)
- android支持百分比的屏幕适配
- 屏幕适配之百分比方案详解
- 屏幕百分比适配的属性
- 屏幕适配之百分比方案详解
- 万能适配,百分比布局,支持padding
- PHP学习笔记 04
- ordfilt2函数 MATLAB
- 到底有多二
- Linux--环境变量被重置
- 设计模式——之适配器模式
- 屏幕适配和百分比布局
- 草根seo人为什么那么苦逼?
- 各种Loss Function的比较
- 美国国防部发明机械手臂,助老兵恢复正常生活!
- Python的mysql操作(安装、链接)
- 《七点三刻》有价值的新闻早餐 2017年11月21日星期二,第012期
- python下的MySQL安装
- Windows操作系统网络参数方面的一些文章
- 接口自动化测试PHPUnit-框架代码开发3