日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android設置Padding和Margin(動態/靜態)的方法實例_Android

作者:&歲月不待人& ? 更新時間: 2022-12-24 編程語言

一、什么是padding,什么是margin?

在Android界面開發時,為了布局更加合理好看,很多時候會用上Padding和Margin,

padding和margin是什么呢?即內邊距和外邊距;

某個View指定為padding是針對該View里面的子View距離該View距離而言的,或者是里面的內容距離容器的距離。

某個View指定為margin是針對該View本身距離別人或者父View而言的。

例如下圖,輸入框里面的文字內容,如果不設置內邊距,那么就會緊挨左上角,這樣看起來,就很不友好,合理的設置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" />

?二、動態設置邊距

那么怎么動態設置padding和margin呢?其實也很簡單。

1.設置padding

view.setPadding(int left, int top, int right, int bottom)//view為你要設置的控件

例子:在我點擊搜索框后,搜索框獲取焦點,準備輸入內容的時候,圖標消失,文本內邊距修改,實現代碼如下

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
            }
        }

實現效果:最開始文本里左邊內邊距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手機廠商很多,導致了不同設備屏幕大小和分辨率都不一樣,然而我們開發者要保持在不同設備上顯示同樣的視覺效果,就需要做一些適配效果。

相關名詞解釋

  • 屏幕大小:通常指的是屏幕對角線的長度,使用“寸”為單位來衡量。
  • 分辨率:指手機屏幕的像素點個數,例如:720*1280,指的是寬有720個像素點,高有1280個像素點。
  • dpi:指的是每英寸像素,是由對角線上的像素點數除以屏幕大小所得。

系統屏幕密度

  • ldpi文件夾下對應的密度為120dpi,對應的分辨率為240*320
  • mdpi文件夾下對應的密度為160dpi,對應的分辨率為320*480
  • hdpi文件夾下對應的密度為240dpi,對應的分辨率為480*800
  • xhdpi文件夾下對應的密度為320dpi,對應的分辨率為720*1280
  • xxhdpi文件夾下對應的密度為480dpi,對應的分辨率為1080*1920

由于各種屏幕密度的不同,導致了同一張圖片在不同的手機屏幕上顯示不同;在屏幕大小相同的情況下,高密度的屏幕包含了更多的像素點。android系統將密度為160dpi的屏幕作為標準對于mdpi文件夾,在此屏幕的手機上1dp=1px。從上面系統屏幕密度可以得出各個密度值之間的換算;在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);
? ? }

利用系統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.動態設置margin

android的view中有setPadding,但是沒有直接的setMargin方法。如果要在代碼中設置該怎么做呢?可以通過設置view里面的?LayoutParams 設置,而這個LayoutParams是根據該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

總結

原文鏈接:https://blog.csdn.net/LoveFHM/article/details/124805791

欄目分類
最近更新