dependencies {
...
compile 'com.zhy:percent-support-extends:1.1.1'
}
至於這個庫,詳細地址在這裏,大家可以到這裏去查看https://bintray.com/hongyangandroid/maven/android-screen-support-ext/view#files/com/zhy/percent-support-extendslayout_marginPercent、layout_marginLeftPercent、
layout_marginTopPercent、layout_marginRightPercent、
layout_marginBottomPercent、layout_marginStartPercent、layout_marginEndPercent。
layout_textSizePercent
<com.zhy.android.percent.support.PercentFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<com.zhy.android.percent.support.PercentFrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ff1234"
android:layout_gravity="center"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%">
<com.zhy.android.percent.support.PercentFrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#50b9b6"
android:layout_gravity="center"
app:layout_heightPercent="50%w"
app:layout_widthPercent="50%w">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_marginPercent="10%"
android:background="#12ff5d"
android:text="text margin 10%"
app:layout_textSizePercent="10%"
android:gravity="center"
/>
</com.zhy.android.percent.support.PercentFrameLayout>
</com.zhy.android.percent.support.PercentFrameLayout>
</com.zhy.android.percent.support.PercentFrameLayout>
他的實現效果是當寬高設定的時候同時以父佈局的寬或者是高來給定百分比就可以來設定了
<com.zhy.android.percent.support.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:id="@+id/text1"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_heightPercent="30%"
app:layout_widthPercent="70%"
android:background="#49ab8a"
android:gravity="center"
android:text="W 70% H 30%"/>
<TextView
android:id="@+id/text2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_heightPercent="10%"
app:layout_widthPercent="10%"
android:background="#efe17b"
android:layout_toRightOf="@+id/text1"/>
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_heightPercent="20%"
app:layout_widthPercent="60%"
android:background="#c55f40"
android:layout_below="@+id/text1"
app:layout_marginLeftPercent="10%w"
android:gravity="center"
android:text="W 60% H 20% M 10%"/>
</com.zhy.android.percent.support.PercentRelativeLayout>
他的實現效果
<ScrollView android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<com.zhy.android.percent.support.PercentLinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ff44aacc"
android:gravity="center"
android:text="width:60%,height:5%,ts:3%"
android:textColor="#ffffff"
app:layout_heightPercent="5%"
app:layout_marginBottomPercent="5%"
app:layout_textSizePercent="3%"
app:layout_widthPercent="60%"/>
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ff4400cc"
android:gravity="center"
android:text="width:70%,height:10%"
android:textColor="#ffffff"
app:layout_heightPercent="10%"
app:layout_marginBottomPercent="5%"
app:layout_widthPercent="70%"/>
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ff44aacc"
android:gravity="center"
android:text="w:80%,h:15%,textSize:5%"
android:textColor="#ffffff"
app:layout_heightPercent="15%"
app:layout_marginBottomPercent="5%"
app:layout_textSizePercent="5%"
app:layout_widthPercent="80%"/>
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ff4400cc"
android:gravity="center"
android:text="width:90%,height:5%"
android:textColor="#ffffff"
app:layout_heightPercent="20%"
app:layout_marginBottomPercent="5%"
app:layout_widthPercent="90%"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#ff44aacc"
android:gravity="center"
android:text="width:100%,height:25%"
android:textColor="#ffffff"
app:layout_heightPercent="25%"
app:layout_marginBottomPercent="5%"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#ff44aacc"
android:gravity="center"
android:text="width:100%,height:30%"
android:textColor="#ffffff"
app:layout_heightPercent="30%"
app:layout_marginBottomPercent="5%"
/>
</com.zhy.android.percent.support.PercentLinearLayout>
</ScrollView>
這裏LinearLayout和Scrollview結合使用,那麼我們這裏的百分比就是整個屏幕的寬高了
但是還要說的是,我們適配的話圖片怎麼辦,圖片適配如何實現呢?圖片也可以像上面那樣控制大小,但是有一些問題是這個庫也無法解決的,所以接下來要看如何進行圖片適配。
<ImageView
android:id="@+id/imageView"
android:layout_width="426px"
android:layout_height="240px"
android:src="@mipmap/ceshi"/>
爲了方便起見並且利用上面的庫,我們在他旁邊設置一個帶背景的TextView寬度爲60.5是經過計算在1920*1080分辨率下除去圖片寬度所剩下的大致百分比,也就是說如果換了分辨率圖片和TextView仍然不重合那麼就說明適配成功了。
//標準切圖得到的分辨率寬度
private static final float BASE_SCREEN_WIDTH_FLOAT = 1080;
然後得到屏幕實際分辨率得到縮放比displayMetrics = getResources().getDisplayMetrics();
int widthPixels = displayMetrics.widthPixels;
scale = (float)widthPixels / BASE_SCREEN_WIDTH_FLOAT;
然後我們進行縮放就行
public void scaleViewSize(View view) {
if (null != view) {
//利用縮放比得到實際應該設置的padding也就是確定他的位置
int paddingLeft = getScaleValue(view.getPaddingLeft());
int paddingTop = getScaleValue(view.getPaddingTop());
int paddingRight = getScaleValue(view.getPaddingRight());
int paddingBottom = getScaleValue(view.getPaddingBottom());
view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
if (null != layoutParams) {
if (layoutParams.width > 0) {
layoutParams.width = getScaleValue(layoutParams.width);
}
if (layoutParams.height > 0) {
layoutParams.height = getScaleValue(layoutParams.height);
}
if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) layoutParams;
int topMargin = getScaleValue(marginLayoutParams.topMargin);
int leftMargin = getScaleValue(marginLayoutParams.leftMargin);
int bottomMargin = getScaleValue(marginLayoutParams.bottomMargin);
int rightMargin = getScaleValue(marginLayoutParams.rightMargin);
marginLayoutParams.topMargin = topMargin;
marginLayoutParams.leftMargin = leftMargin;
marginLayoutParams.bottomMargin = bottomMargin;
marginLayoutParams.rightMargin = rightMargin;
}
}
view.setLayoutParams(layoutParams);
}
}
上個方法調用的生成適配後數據的方法private int getScaleValue(float topMargin) {
int result = (int) (scale*topMargin);
return result;
}
這樣我們圖片適配也就完成了。感謝谷歌的小弟和鴻洋大神的博客~