android動(dòng)態(tài)加載布局文件示例
一、布局文件part.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:text="添加" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
二、通過(guò)后臺(tái)代碼生成前臺(tái)布局:
package com.example.codeui;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout=new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);//顯示方向
//將view對(duì)象添加到布局界面
TextView textView =new TextView(this);
textView.setText("Hello Code UI");
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
//添加
layout.addView(textView,params);
//添加外部xml定義的布局
View view = getPartView();
layout.addView(view);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);
setContentView(layout, layoutParams);
//setContentView(R.layout.activity_main);
//采用代碼編寫效率高,但是很難維護(hù)
}
//通過(guò)加載xml文件將view添加到布局中
public View getPartView() {
//將xml布局文件生成view對(duì)象通過(guò)LayoutInflater
LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//將view對(duì)象掛載到那個(gè)父元素上,這里沒(méi)有就為null
return inflater.inflate(R.layout.part, null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
相關(guān)文章
Android彈窗ListPopupWindow的簡(jiǎn)單應(yīng)用詳解
這篇文章主要為大家詳細(xì)介紹了Android彈窗ListPopupWindow的簡(jiǎn)單應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Android 登錄頁(yè)面的實(shí)現(xiàn)代碼(密碼顯示隱藏、EditText 圖標(biāo)切換、限制輸入長(zhǎng)度)
這篇文章主要介紹了Android 登錄頁(yè)面的實(shí)現(xiàn)代碼(密碼顯示隱藏、EditText 圖標(biāo)切換、限制輸入長(zhǎng)度),本文通過(guò)兩種方法給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08android與asp.net服務(wù)端共享session的方法詳解
這篇文章主要給大家介紹了關(guān)于android與asp.net服務(wù)端如何共享session的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)下吧。2017-09-09Android自定義view之利用drawArc方法實(shí)現(xiàn)動(dòng)態(tài)效果(思路詳解)
這篇文章主要介紹了Android自定義view之利用drawArc方法實(shí)現(xiàn)動(dòng)態(tài)效果,drawArc方法包含了五個(gè)參數(shù),具體細(xì)節(jié)在本文中給大家提到過(guò),需要的朋友可以參考下2021-08-08Android 退出多Activity的application的方式方法
在開發(fā)過(guò)程中,我們常常需要一個(gè)退出功能,來(lái)退出該應(yīng)用的所有Activity,本篇文章主要介紹了Android 退出多Activity的application的方式,有興趣的可以了解一下。2017-02-02Android開發(fā)之Activity全透明漸變切換方法
下面小編就為大家分享一篇Android開發(fā)之Activity全透明漸變切換方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Android webview注入JS代碼 修改網(wǎng)頁(yè)內(nèi)容操作
這篇文章主要介紹了Android webview注入JS代碼 修改網(wǎng)頁(yè)內(nèi)容操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03