Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android,利用layoutParams代碼動態布局空間位置

android,利用layoutParams代碼動態布局空間位置

編輯:關於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)
		Map markJsonMap = (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 由底層到外層進行布局的 ,應該在底層布局之上 \
2.在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);

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved