导包 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百分比布局详解
941

被折叠的 条评论
为什么被折叠?



