移动App和远程服务器笔记(3)

Android中动态添加View的两种方法


一、使用xml的方式:

1、LayoutInflater:

这个类可以把xml表述的Layout解析为View,从而可以使addView()方法添加View。

2、LayoutInflater与findViewById的区别:

两者都是实例化某一个对象,不同的是findViewById是通过找xml布局文件下的一个具体的widget控件进行实例化,而LayoutInflater是找res/layout 下的xml布局文件来实例化的。

3、使用方法:

LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);或

LayoutInflater inflater=LayoutInflater.from(Activity.this);或

LayoutInflater inflater=getLayoutInflater();

这三种方式本质上是相同的。

4、inflate():

使用inflate()可以将xml解析为View

inflaer.inflate(R.layout.xml文件名,parent);

范例:
main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </LinearLayout>

</LinearLayout>

view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello word" />

</LinearLayout>

Main.java

public class Main extends Activity {

@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  addView1();
  addView2();
  addView3();
  addView4();
 }

private void addView1() {
  LinearLayout container = (LinearLayout) findViewById(R.id.container);
  LayoutInflater inflater = this.getLayoutInflater();
  LinearLayout view = (LinearLayout) inflater
    .inflate(R.layout.view, null);
  container.addView(view);
 }

private void addView2() {
  LinearLayout container = (LinearLayout) findViewById(R.id.container);
  LayoutInflater inflater = LayoutInflater.from(this);
  LinearLayout view = (LinearLayout) inflater
    .inflate(R.layout.view, null);
  container.addView(view);
 }

private void addView3() {
  LinearLayout container = (LinearLayout) findViewById(R.id.container);
  LayoutInflater inflater = (LayoutInflater) this
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  LinearLayout view = (LinearLayout) inflater
    .inflate(R.layout.view, null);
  container.addView(view);
 }

private void addView4() {
  LinearLayout container = (LinearLayout) findViewById(R.id.container);
  LinearLayout view = this.getLayoutInflater().inflate(R.layout.view,
    container);
 }

}

四个addView()方法本质上都是相同的。

二、使用java的方式:

private void addViewByJava() {
    LinearLayout container = new LinearLayout(this);//主布局container
    TextView tv = new TextView(this);//子View TextView
    // 为主布局container设置布局参数
    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
      LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    container.setLayoutParams(llp);//设置container的布局
    container.setOrientation(LinearLayout.HORIZONTAL);// 设置主布局的orientation
    // 为子View设置布局参数
    ViewGroup.LayoutParams vlp = new ViewGroup.LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT,
      ViewGroup.LayoutParams.WRAP_CONTENT);
    tv.setLayoutParams(vlp);// 设置TextView的布局
    tv.setText("hello word");
    container.addView(tv);// 将TextView 添加到container中
   }

原地址:http://www.cnblogs.com/joeleedreamer/p/4662503.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值