Android設置Padding和Margin(動態(tài)/靜態(tài))的方法實例
一、什么是padding,什么是margin?
在Android界面開發(fā)時,為了布局更加合理好看,很多時候會用上Padding和Margin,
padding和margin是什么呢?即內(nèi)邊距和外邊距;
某個View指定為padding是針對該View里面的子View距離該View距離而言的,或者是里面的內(nèi)容距離容器的距離。
某個View指定為margin是針對該View本身距離別人或者父View而言的。
例如下圖,輸入框里面的文字內(nèi)容,如果不設置內(nèi)邊距,那么就會緊挨左上角,這樣看起來,就很不友好,合理的設置padding看起來會舒服很多。
如果,不設置外邊距,會充滿整個父布局,也不好看,這時候就需要margin屬性(外邊距)。
類似于控件的基礎屬性,并且不會變化的,我們一般會直接在xml文件里直接設置,這是上圖的布局代碼
<androidx.appcompat.widget.AppCompatEditText android:id="@+id/chat_input_edit" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginStart="10dp" android:layout_marginTop="10dp" android:layout_marginEnd="10dp" android:layout_marginBottom="10dp" android:paddingHorizontal="12dp" android:paddingVertical="10dp" android:textColor="@color/white" android:textColorHint="#94ffffff" android:textSize="14sp" />
二、動態(tài)設置邊距
那么怎么動態(tài)設置padding和margin呢?其實也很簡單。
1.設置padding
view.setPadding(int left, int top, int right, int bottom)//view為你要設置的控件
例子:在我點擊搜索框后,搜索框獲取焦點,準備輸入內(nèi)容的時候,圖標消失,文本內(nèi)邊距修改,實現(xiàn)代碼如下
editText.setOnFocusChangeListener { view, b -> if (b) {//使用dp2px方法進行屏幕適配 view.setPadding(DPUtils.dp2px(12f),DPUtils.dp2px(6f),DPUtils.dp2px(12f),DPUtils.dp2px(6f)) searchIcon.visibility = View.GONE } }
實現(xiàn)效果:最開始文本里左邊內(nèi)邊距32dp,點擊后變成12dp
//這是dp轉為px的方法 private fun dp2px(i: Int): Int { return (Resources.getSystem().displayMetrics.density * i + 0.5f).toInt()}
為什么會有dp2px這個方法來轉一下呢?
附:android中px與sp,dp之間的轉換
由于Android手機廠商很多,導致了不同設備屏幕大小和分辨率都不一樣,然而我們開發(fā)者要保持在不同設備上顯示同樣的視覺效果,就需要做一些適配效果。
相關名詞解釋
- 屏幕大小:通常指的是屏幕對角線的長度,使用“寸”為單位來衡量。
- 分辨率:指手機屏幕的像素點個數(shù),例如:720*1280,指的是寬有720個像素點,高有1280個像素點。
- dpi:指的是每英寸像素,是由對角線上的像素點數(shù)除以屏幕大小所得。
系統(tǒng)屏幕密度
- ldpi文件夾下對應的密度為120dpi,對應的分辨率為240*320
- mdpi文件夾下對應的密度為160dpi,對應的分辨率為320*480
- hdpi文件夾下對應的密度為240dpi,對應的分辨率為480*800
- xhdpi文件夾下對應的密度為320dpi,對應的分辨率為720*1280
- xxhdpi文件夾下對應的密度為480dpi,對應的分辨率為1080*1920
由于各種屏幕密度的不同,導致了同一張圖片在不同的手機屏幕上顯示不同;在屏幕大小相同的情況下,高密度的屏幕包含了更多的像素點。android系統(tǒng)將密度為160dpi的屏幕作為標準對于mdpi文件夾,在此屏幕的手機上1dp=1px。從上面系統(tǒng)屏幕密度可以得出各個密度值之間的換算;在mdpi中1dp=1px,在hdpi中1dp=1.5px,在xhdpi中1dp=2px,在xxhpi中1dp=3px。換算比例如下:ldpi:mdpi:hdpi:xhdpi:xxhdpi=3:4:6:8:12。
單位換算方法
/** ? ? ?* dp轉換成px ? ? ?*/ ? ? private int dp2px(Context context,float dpValue){ ? ? ? ? float scale=context.getResources().getDisplayMetrics().density; ? ? ? ? return (int)(dpValue*scale+0.5f); ? ? } ? ? /** ? ? ?* px轉換成dp ? ? ?*/ ? ? private int px2dp(Context context,float pxValue){ ? ? ? ? float scale=context.getResources().getDisplayMetrics().density; ? ? ? ? return (int)(pxValue/scale+0.5f); ? ? } ? ? /** ? ? ?* sp轉換成px ? ? ?*/ ? ? private int sp2px(Context context,float spValue){ ? ? ? ? float fontScale=context.getResources().getDisplayMetrics().scaledDensity; ? ? ? ? return (int) (spValue*fontScale+0.5f); ? ? } ? ? /** ? ? ?* px轉換成sp ? ? ?*/ ? ? private int px2sp(Context context,float pxValue){ ? ? ? ? float fontScale=context.getResources().getDisplayMetrics().scaledDensity; ? ? ? ? return (int) (pxValue/fontScale+0.5f); ? ? }
利用系統(tǒng)TypeValue類來轉換
private int dp2px(Context context,int dpValue){ return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpValue,context.getResources().getDisplayMetrics()); } private int sp2px(Context context,int spValue){ return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spValue,context.getResources().getDisplayMetrics()); }
2.動態(tài)設置margin
android的view中有setPadding,但是沒有直接的setMargin方法。如果要在代碼中設置該怎么做呢?可以通過設置view里面的 LayoutParams 設置,而這個LayoutParams是根據(jù)該view在不同的GroupView而不同的。這兒用的是RelativeLayout是因為在他的父布局是RelativeLayout哦,用成其他的會報錯哦~~
val lp = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT) lp.setMargins(0, 0, DPUtils.dp2px(7f), DPUtils.dp2px(7f)) //RelativeLayout可以通過LayoutParams的addRule來添加約束,其他的布局也有類似的一些方法 lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM) lp.addRule(RelativeLayout.ALIGN_PARENT_END) textView.layoutParams = lp
總結
到此這篇關于Android設置Padding和Margin(動態(tài)/靜態(tài))的文章就介紹到這了,更多相關Android設置Padding和Margin內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android多設備多module打包fat-aar(最新推薦)
這篇文章主要介紹了Android多設備多module打包(fat-aar),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03Android中okhttp3.4.1+retrofit2.1.0實現(xiàn)離線緩存
這篇文章主要介紹了Android中okhttp3.4.1結合retrofit2.1.0實現(xiàn)離線緩存,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10Android實現(xiàn)底部圖標與Fragment的聯(lián)動實例
本篇文章主要介紹了Android實現(xiàn)底部圖標與Fragment的聯(lián)動實例,具有一定的參考價值,有興趣的可以了解一下2017-07-07