加载layout
??? 近期對android里面控件修改做了很多實驗,由于公司需求很多,不得不重寫很多控件。程序目標無非是:高效、輕巧、清晰、標準化
?? ?完成動態加載Layout有兩種方法,依據個人喜好進行選擇:
方法1:靜態主Layout動態加載靜態子Layout
?
?
?
?
首先構建子Layout:main2
[xhtml]view plaincopy
<?xml version="1.0" encoding="utf-8"?> ?
<!--布局可以任意定義,此處拿線性布局舉例,里面有2個按鈕元素--> ?
<LinearLayout? xmlns:android="http://schemas.android.com/apk/res/android" ?
??? android:id="@+id/menubar"? ?
??? android:background="@drawable/menubar" ?
??? android:layout_width="wrap_content"? ?
??? android:layout_height="wrap_content"> ?
??????? <!--按鈕1--> ?
??? <ImageButton android:id="@+id/button1" ?
??????? android:src="@drawable/btn1" ?
??????? android:layout_width="wrap_content" ?
??????? android:layout_height="wrap_content" ?
??? ></ImageButton> ?
??? <!--按鈕2--> ?
??? <ImageButton android:id="@+id/button2" ?
??????? android:src="@drawable/btn2" ?
??????? android:layout_width="wrap_content" ?
??????? android:layout_height="wrap_content" ?
??? ></ImageButton> ?
</LinearLayout> ?
?
?
然后構建主Layout:main
?
[xhtml]view plaincopy
<?xml version="1.0" encoding="utf-8"?> ?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ?
??? android:id="@+id/background" ?
??? android:layout_width="fill_parent" ?
??? android:layout_height="fill_parent" ?
??? android:background="@drawable/background"> ?
??? <!--主Layout要給子Layout設置一個容器box,可以在此指定容器的位置,這段是關鍵部分--> ?
?? ?<LinearLayout
?? ?android:id="@+id/box" ?
??????? android:layout_alignParentBottom="true" ?
??????? android:layout_width="wrap_content" ?
??????? android:layout_height="wrap_content" ?
??????? android:layout_centerHorizontal="true">???? ?
??? </LinearLayout> ?
</RelativeLayout> ?
?
?
最后在程序中加載子layout:
[java]view plaincopy
public class BackgroundTest extends Activity { ?
??? private View mBarView; ??? ??? ??? ??? ??? ??? ??? ?//????? 子Layout要以view的形式加入到主Layout中
??? private LinearLayout mLinearLayout; ??? ??? ??? ??? ?//????? 主Layout的容器加載子Layout的View ?
?? ?public void onCreate(Bundle savedInstanceState) { ?
??????? super.onCreate(savedInstanceState); ?
//????? 顯示主Layout ?
??????? setContentView(R.layout.main);
//????? 加載子Layout??????? ?
??????? mBarView = View.inflate(this, R.layout.main2, null); ?
?? ??? ?
//????? 找到容器 ?
??????? mLinearLayout = (LinearLayout)findViewById(R.id.box); ?
//????? 加上View 結束 ?
??????? mLinearLayout.addView(mBarView); ?
} ?
?
?
?
方法2:靜態主Layout動態加載動態的Layout
?? ?首先構造你自己的子Layout和上面一樣;
?? ?然后構建你自定義的Layout類:
[java]view plaincopy
public class MenuLandscapeLinearLayout extends LinearLayout{? ?
?? ?//? 構造函數 ?
??? public MenuLandscapeLinearLayout(Context context) { ?
??? super(context); ?
??? // TODO Auto-generated constructor stub ?
??? //加載需要的屬性,加載方法一的子Layout ?
??? ((Activity) getContext()).getLayoutInflater().inflate(R.layout.main2, this);? ?
??????? //在此你可以封裝很多方法? ?
??? }??? ?
} ?
?
最后在程序中動態實例化并加載即可:
[java]view plaincopy
public class BackgroundTest extends Activity { ?
??? /** Called when the activity is first created. */ ?
??? private LinearLayout mLinearLayout; ?
??? //聲明一個子Layout View對象 ?
??? private MenuLandscapeLinearLayout mMenuLandscapeLinearLayout; ?
??? @Override ?
??? public void onCreate(Bundle savedInstanceState) { ?
??????? super.onCreate(savedInstanceState); ?
//????? 加載主Layout ?
??????? setContentView(R.layout.main);?? ?
//????? 找到容器?????? ?
??????? mLinearLayout = (LinearLayout)findViewById(R.id.box); ?
//????? 實例化一個子View ?
??????? mMenuLandscapeLinearLayout=new MenuLandscapeLinearLayout(this); ?
//????? 添加到容器 ?
??????? mLinearLayout.addView(mMenuLandscapeLinearLayout); ?
??? } ?
} ?
轉載于:https://www.cnblogs.com/carbs/archive/2012/07/02/2573642.html
總結
- 上一篇: Visual Studio的导入和导出设
- 下一篇: intel ssd toolbox 绿色