New UI-Java代碼動態添加控制項或xml布局,ui-javaxml
New UI-Java代碼動態添加控制項或xml布局
——轉載請註明出處:coder-pig,歡迎轉載,請勿用於商業用途!
小豬Android開發交流群已建立,歡迎大家加入,無論是新手,菜鳥,大神都可以,小豬一個人的
力量畢竟是有限的,寫出來的東西肯定會有很多紕漏不足,歡迎大家指出,集思廣益,讓小豬的博文
更加的詳盡,幫到更多的人,O(∩_∩)O謝謝!
小豬Android開發交流群:小豬Android開發交流群群號:421858269
新Android UI執行個體大全目錄:http://blog.csdn.net/coder_pig/article/details/42145907
本節引言:
上一節我們學過了純Java代碼來載入布局,已經有了一點動態布局的基礎了,本節中
我們講解的是,在載入了xml的基礎上,來動態地添加View控制項!以及動態地載入XML
布局!
本節本文:
1.Java代碼動態添加控制項:
動態添加組件的寫法有兩種,區別在於是否需要先setContentView(R.layout.activity_main);
下面示範動態地為介面添加一個Button
activity_main.xml檔案的布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.jay.example.trendsinflateviewdemo.MainActivity" > <TextView android:id="@+id/txtTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我是xml檔案載入的布局"/> </RelativeLayout>
第一種:不需要setContentView();
package com.jay.example.trendsinflateviewdemo;import android.app.Activity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.RelativeLayout;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Button btnOne = new Button(this);btnOne.setText("我是動態添加的按鈕");RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); lp2.addRule(RelativeLayout.CENTER_IN_PARENT); LayoutInflater inflater = LayoutInflater.from(this);RelativeLayout rly = (RelativeLayout) inflater.inflate(R.layout.activity_main, null).findViewById(R.id.RelativeLayout1);rly.addView(btnOne,lp2);setContentView(rly);}}
第二種:需要setContentView();
package com.jay.example.trendsinflateviewdemo;import android.app.Activity;import android.os.Bundle;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.RelativeLayout;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btnOne = new Button(this);btnOne.setText("我是動態添加的按鈕");RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); lp2.addRule(RelativeLayout.CENTER_IN_PARENT); RelativeLayout rly = (RelativeLayout) findViewById(R.id.RelativeLayout1);rly.addView(btnOne,lp2);}}
分析總結:
代碼很簡單,建立按鈕後,我們又建立了一個LayoutParams對象,用來設定Button的大小,又通過
addRule()方法設定了Button的位置!
第一種方法:通過LayoutInflate的inflate( )方法載入了activity_main布局,獲得了外層容器,接著
addView添加按鈕進容器,最後setContentView();
第二種方法:因為我們已經通過setContetView()方法載入了布局,此時我們就可以通過findViewById
找到這個外層容器,接著addView,最後setContentView()即可!
另外,關於這個setContentView( )他設定的視圖節點是整個XML的根節點!
關於這個動態添加控制項其實也不難,如果你看了前面那一篇<純Java載入布局>的話,
2.Java代碼動態載入xml布局
接下來的話,我們換一個,這次載入的是xml檔案!動態地添加xml檔案!
先寫下布局檔案吧:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.jay.example.trendsinflateviewdemo.MainActivity" > <Button android:id="@+id/btnLoad" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="動態載入布局"/> </RelativeLayout>
動態載入的xml檔案inflate.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:id="@+id/ly_inflate" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是Java代碼載入的布局" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是布局裡的一個小按鈕" /></LinearLayout>
接著就到我們的MainActivity.java了:
package com.jay.example.trendsinflateviewdemo;import android.app.Activity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.LinearLayout;import android.widget.RelativeLayout;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//獲得LayoutInflater對象;final LayoutInflater inflater = LayoutInflater.from(this); //獲得外部容器物件final RelativeLayout rly = (RelativeLayout) findViewById(R.id.RelativeLayout1);Button btnLoad = (Button) findViewById(R.id.btnLoad);btnLoad.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//載入要添加的布局對象LinearLayout ly = (LinearLayout) inflater.inflate(R.layout.inflate_ly, null, false).findViewById(R.id.ly_inflate);//設定載入布局的大小與位置RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.CENTER_IN_PARENT); rly.addView(ly,lp);}});}}
運行:
接下來就來分析下代碼:
①擷取容器物件:
final RelativeLayout rly = (RelativeLayout) findViewById(R.id.RelativeLayout1);
②獲得Inflater對象,同時載入被添加的布局的xml,通過findViewById找到最外層的根節點
final LayoutInflater inflater = LayoutInflater.from(this);
LinearLayout ly = (LinearLayout) inflater.inflate(
R.layout.inflate_ly, null, false).findViewById(
R.id.ly_inflate);
③為這個容器設定大小與位置資訊:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
④添加到外層容器中:
rly.addView(ly,lp);
好了,關於通過Java代碼動態地添加控制項或XML的內容大概就講到這裡!
另外,提醒一點,addView( )後,當這個View不需要的使用可以通過RemoveView( )將
這個View移除哦!