編輯:關於Android編程
Android開發:LayoutParams的用法
LayoutParams繼承於Android.View.ViewGroup.LayoutParams.
LayoutParams相當於一個Layout的信息包,它封裝了Layout的位置、高、寬等信息。假設在屏幕上一塊區域是由一個Layout占領的,如果將一個View添加到一個Layout中,最好告訴Layout用戶期望的布局方式,也就是將一個認可的layoutParams傳遞進去。
可以這樣去形容LayoutParams,在象棋的棋盤上,每個棋子都占據一個位置,也就是每個棋子都有一個位置的信息,如這個棋子在4行4列,這裡的“4行4列”就是棋子的LayoutParams。
但LayoutParams類也只是簡單的描述了寬高,寬和高都可以設置成三種值:
1,一個確定的值;
2,FILL_PARENT,即填滿(和父容器一樣大小);
3,WRAP_CONTENT,即包裹住組件就好。
在JAVA中動態構建的布局,常常這樣寫:
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
上面這一句話其實是子對父的,也就是說,父布局下的子控件要設置這句話。 因為布局很多,雖然都繼承至ViewGroup但是各個布局還是有很大的不同。 很顯然上面這句應該這樣寫才算准確:
setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT));
這表示這個子控件的父布局是一個TableRow , 這樣的LayoutParams 太多,所以應明確指明。 下面分別說下兩個常用到布局: 1. FrameLayout下動態設置子控件居中,動態用JAVA代碼要這樣實現:這表示這個子控件的父布局是一個TableRow , 這樣的LayoutParams 太多,所以應明確指明。 下面分別說下兩個常用到布局: 1. FrameLayout下動態設置子控件居中,動態用JAVA代碼要這樣實現:
FrameLayout.LayoutParams lytp = new FrameLayout.LayoutParams(80,LayoutParams.WRAP_CONTENT); lytp .gravity = Gravity.CENTER; btn.setLayoutParams(lytp);
2. RelativeLayout下動態設置子控件居中:
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); btn1.setLayoutParams(lp);
實例:
從網頁響應獲取Json 對象,並解析
@SuppressWarnings(unchecked) MapmarkJsonMap = (Map ) mapJson.get(adlogo); if(markJsonMap != null){ String position = (String) markJsonMap.get(position); String markTumbString = (String) markJsonMap.get(thumb); String smlscreen = (String) markJsonMap.get(smlscreen); String bigscreen = (String) markJsonMap.get(bigscreen); HomeActivity.mCornerMark.setPosition(position); HomeActivity.mCornerMark.setThumb(markTumbString); HomeActivity.mCornerMark.setSmlscreen(smlscreen); HomeActivity.mCornerMark.setBigscreen(bigscreen); setCornerMark(); }
/** * 動態設置角標位置 * */ private void setCornerMark(){ if(!HomeActivity.mCornerMark.getThumb().equals() && HomeActivity.mCornerMark.getThumb().length() != 0) { // WindowManager wm = (WindowManager) mContex // .getSystemService(Context.WINDOW_SERVICE); // // int width = wm.getDefaultDisplay().getWidth(); // int height = wm.getDefaultDisplay().getHeight(); FrameLayout.LayoutParams reParams = (android.widget.FrameLayout.LayoutParams) HomeActivity.home_surface_viewLyout.getLayoutParams(); int width = reParams.width; int height = reParams.height; RelativeLayout.LayoutParams params; //width, height * 48 / 128 //獲取控件布局參數 Configuration config = mContex.getResources().getConfiguration(); if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { String[] paramString = HomeActivity.mCornerMark.getBigscreen().split(,); int whidthString = Integer.parseInt(paramString[0]); int heightString = Integer.parseInt(paramString[1]); //轉dp whidthString = DensityUtil.px2dip(mContex,whidthString); heightString = DensityUtil.px2dip(mContex,heightString); params = new LayoutParams(whidthString,heightString); }else{ String[] paramString = HomeActivity.mCornerMark.getSmlscreen().split(,); int whidthString = Integer.parseInt(paramString[0]); int heightString = Integer.parseInt(paramString[1]); //轉dp whidthString = (int) (width*0.005*whidthString); heightString = (int) (height*0.005*heightString); whidthString = DensityUtil.px2dip(mContex,whidthString); heightString = DensityUtil.px2dip(mContex,heightString); params = new RelativeLayout.LayoutParams(whidthString,heightString); } //動態指定控件大小,位置 Log.v(Position, HomeActivity.mCornerMark.getPosition()); if(HomeActivity.mCornerMark.getPosition().equals(1)){ params.leftMargin=20; params.topMargin=20; params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); } else if(HomeActivity.mCornerMark.getPosition().equals(2)){ params.rightMargin=20; params.topMargin=20; params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); }else if (HomeActivity.mCornerMark.getPosition().equals(3)) { params.leftMargin=20; params.bottomMargin=20; params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); }else if(HomeActivity.mCornerMark.getPosition().equals(4)){ params.rightMargin=20; params.bottomMargin=20; params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); } HomeActivity.img_cornermark.setLayoutParams(params); DownLoadTask downLoadTask = new DownLoadTask(HomeActivity.img_cornermark); downLoadTask.execute(HomeActivity.mCornerMark.getThumb()); } }
1. 在RelativeLayout裡的布局,圖片不顯示?
RelativeLayout 層次布局是通過xml 文件 Relativelayout 由底層到外層進行布局的 ,應該在底層布局之上params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
3.不同手機分辨率等比列顯示:
String[] paramString = HomeActivity.mCornerMark.getSmlscreen().split(,); int whidthString = Integer.parseInt(paramString[0]); int heightString = Integer.parseInt(paramString[1]); //轉dp whidthString = (int) (width*0.005*whidthString); heightString = (int) (height*0.005*heightString); whidthString = DensityUtil.px2dip(mContex,whidthString); heightString = DensityUtil.px2dip(mContex,heightString); params = new RelativeLayout.LayoutParams(whidthString,heightString);
平時對牛逼動畫,高級UI,都深入的不多!近日,某頭條,推了一個android技術類視頻(平時在頭條關注技術比較多),講的是加載動畫效果,是動腦學院講的公開課,160分鐘,
第4節 遠程調用之前提到過:如果站在Service與觸發Service運行的那個組件的角度,根據它們的關系進行分類,有兩種:本地Service,遠程Service。本地S
之前使用asp.net開發軟件的過程中也使用GridView,發現android裡面也有這麼一個控件,使用方法有點相似,都是使用適配器將數據綁定到這個控件,
Android SpannableString設置超鏈接、顏色、字體等屬性在Android中,TextView是我們最常用的用來顯示文本的控件。 一般情況下,