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);
//采用代碼編寫(xiě)效率高,但是很難維護(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)文章
學(xué)習(xí)使用Material Design控件(一)
這篇文章主要為大家介紹了學(xué)習(xí)使用Material Design控件的詳細(xì)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android Activity中使用Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)與參數(shù)傳遞的方法
這篇文章主要介紹了Android Activity中使用Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)與參數(shù)傳遞的方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了Android中的Activity交互操作相關(guān)技巧,需要的朋友可以參考下2016-07-07
Android開(kāi)發(fā)Jetpack組件ViewModel與LiveData使用講解
Jetpack是一個(gè)由多個(gè)技術(shù)庫(kù)組成的套件,可幫助開(kāi)發(fā)者遵循最佳做法,減少樣板代碼并編寫(xiě)可在各種Android版本和設(shè)備中一致運(yùn)行的代碼,讓開(kāi)發(fā)者精力集中編寫(xiě)重要的代碼2022-09-09
Android使用Intent發(fā)送短信的實(shí)現(xiàn)方法
這篇文章主要介紹了Android使用Intent發(fā)送短信的實(shí)現(xiàn)方法,結(jié)合簡(jiǎn)單實(shí)例形式分析了Android短信發(fā)送功能的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07
Android編程滑動(dòng)效果之Gallery仿圖像集瀏覽實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程滑動(dòng)效果之Gallery仿圖像集瀏覽實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Gallery瀏覽圖片的原理、步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-02-02
Android Studio 合并module到統(tǒng)一文件夾的方法
這篇文章主要介紹了Android Studio 合并module到統(tǒng)一文件夾的方法,補(bǔ)充介紹了android studio關(guān)于同名資源文件的合并技巧,需要的朋友可以參考下2018-04-04
Android編程心得分享——JSON學(xué)習(xí)過(guò)程
在我們初步學(xué)習(xí)JSON時(shí)我們都知道JSON作為現(xiàn)在比較流行的數(shù)據(jù)交換格式,有著它的許多優(yōu)點(diǎn),這里將我學(xué)習(xí)JSON的過(guò)程記錄如下2013-06-06
Android基礎(chǔ)知識(shí)及線(xiàn)性布局介紹
大家好,本篇文章主要講的是Android基礎(chǔ)知識(shí)及線(xiàn)性布局介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下2022-01-01

