美文网首页
Android中动态添加View

Android中动态添加View

作者: cain07 | 来源:发表于2020-07-20 11:47 被阅读0次

一、使用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);

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="wrap_content"
 3     android:layout_height="wrap_content"
 4     android:orientation="vertical" >
 5 
 6     <LinearLayout
 7         android:id="@+id/container"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content" >
10     </LinearLayout>
11 
12 </LinearLayout>
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="wrap_content"
 3     android:layout_height="wrap_content"
 4     android:orientation="vertical" >
 5 
 6     <TextView
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:text="hello word" />
10 
11 </LinearLayout>

view.xml
1 public class Main extends Activity {
 2 
 3 @Override
 4  protected void onCreate(Bundle savedInstanceState) {
 5   super.onCreate(savedInstanceState);
 6   setContentView(R.layout.main);
 7   addView1();
 8   addView2();
 9   addView3();
10   addView4();
11  }
12 
13 private void addView1() {
14   LinearLayout container = (LinearLayout) findViewById(R.id.container);
15   LayoutInflater inflater = this.getLayoutInflater();
16   LinearLayout view = (LinearLayout) inflater
17     .inflate(R.layout.view, null);
18   container.addView(view);
19  }
20 
21 private void addView2() {
22   LinearLayout container = (LinearLayout) findViewById(R.id.container);
23   LayoutInflater inflater = LayoutInflater.from(this);
24   LinearLayout view = (LinearLayout) inflater
25     .inflate(R.layout.view, null);
26   container.addView(view);
27  }
28 
29 private void addView3() {
30   LinearLayout container = (LinearLayout) findViewById(R.id.container);
31   LayoutInflater inflater = (LayoutInflater) this
32     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
33   LinearLayout view = (LinearLayout) inflater
34     .inflate(R.layout.view, null);
35   container.addView(view);
36  }
37 
38 private void addView4() {
39   LinearLayout container = (LinearLayout) findViewById(R.id.container);
40   LinearLayout view = this.getLayoutInflater().inflate(R.layout.view,
41     container);
42  }
43 
44 }

Main.java

二、使用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中
   }

相关文章

  • Android中动态添加View

    一、使用xml的方式: 1、LayoutInflater: 这个类可以把xml表述的Layout解析为View,从...

  • Learn Flutter Based on Android

    在Android中,addChild和removeChild动态添加或删除View。 在Flutter中,因为wi...

  • Android View.setId(int id) 用法

    Android View.setId(int id) 用法 当要在代码中动态的添加View并且为其设置id时,如果...

  • Id

    Android View.setId(int id) 的用法 当要在代码中动态的添加View并且为其设置id时,如...

  • Android动态添加view

    由于项目需求菜单写活,效果如下: 由于不是可滑动控件,我用的百分比布局做的适配 item_first_type代码...

  • Android动态添加View

    这里以创建一个ImageView为例 1. 首先创建对象 2. 设置对应的属性 3. 创建布局参数 设置子控件在父...

  • Android中自定义view的自定义属性的获取值得问题

    Android开发中,我们在自定义view的时候,通常都是既能让我们的自定义的view在代码中动态添加,又能在xm...

  • 自定义View-LetterView

    1、效果图 2、总体分析 2.1、动态添加View实现   通过在代码中动态添加View的方式,虽然能够实现,但是...

  • Android addview—动态添加view

    一、前言 在日常的开发中经常遇到需要动态添加子view的情况,addview是ViewGroup的特有方法,可以在...

  • 代码中动态添加View

    在布局文件中添加LinearLayout,通过LinearLayout添加高效简单。

网友评论

      本文标题:Android中动态添加View

      本文链接:https://www.haomeiwen.com/subject/fumekktx.html