歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 動態設置控件的布局

Android 動態設置控件的布局

日期:2017/3/1 11:08:59   编辑:Linux編程

在Android開發中,常常會動態地生成些控件,並調調整其布局。那麼,如何動態調整空間的布局,請參閱下面的關鍵代碼:

  1. mRlMain = (RelativeLayout) findViewById(R.id.rlMain);
  2. LayoutParams layoutParams = new LayoutParams(
  3. android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
  4. android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
  5. mBtnView = LayoutInflater.from(mContext).inflate(R.layout.photoright, null);
  6. layoutParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
  7. layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
  8. mRlMain.addView(mBtnView, layoutParams);

主要用到的類有:android.widget.RelativeLayout.LayoutParams,android.view.ViewGroup.LayoutParams及android.widget.RelativeLayout

上面的主要是用相對布局設置,其他布局方式類似。

Copyright © Linux教程網 All Rights Reserved