动态地创建视图 [英] Dynamically creating views

查看:197
本文介绍了动态地创建视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个函数,文字的数组,并创建按钮,并将它们添加到视图。


这是我的code。



工作和创建按钮,但是当我调用该函数两次不创建两个线性布局,它只是显示最后一个称为如果删除第一个。

我怎样才能使它创建一个新的线性布局,并把它添加到视图?

  //创建一个视图
保护布尔CreateTheButtons(字符串[]名称)
{
    尝试
    {
             的LinearLayout linLayout =新的LinearLayout(本);
             linLayout.setOrientation(LinearLayout.HORIZONTAL);
             的LayoutParams linLayoutParam =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
             //设置的LinearLayout作为屏幕的一个根元素
             linLayout.setWeightSum(names.length);
             的setContentView(linLayout,linLayoutParam);             的LayoutParams lpView =新的LayoutParams(0,LayoutParams.WRAP_CONTENT);
             lpView.weight = 1;             的for(int i = 0; I< names.length;我++){             按钮BTN =新按钮(本);
             btn.setText(名称[I]);
             linLayout.addView(BTN,lpView);             }
             返回true;
     }
     赶上(异常前)
     {         返回false;
     }
}


解决方案

  

这是工作和创造的按钮,但是当我调用该函数
  两次它不会创建两个线性布局,它只是显示最后一个
  叫,就好像它是在删除第一个。


您code删除第一个的LinearLayout 源于调用该方法,因为你用的setContentView()(这将取代活动与您作为参数传递视图当前视图(如果有发现))。相反,你应该删除)调用的setContentView(并插入持有人的ViewGroup的 LinearLayouts ,你打算添加通过该方法。

 <! - 这将是活动的内容视图 - >
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:ID =@ + ID /父/>

设置布局的上边,作为该活动的内容来看,在的onCreate()方法:

 的setContentView(R.layout.the_layout_above);

在方法你那么有:

 保护布尔CreateTheButtons(字符串[]名称){
    尝试{
             的LinearLayout linLayout =新的LinearLayout(本);
             linLayout.setOrientation(LinearLayout.HORIZONTAL);
             LinearLayout.LayoutParams linLayoutParam =新LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
             linLayout.setWeightSum(names.length);
             //假设这种方法是在活动
             的LinearLayout父=(的LinearLayout)findViewById(R.id.parent);
             parent.addView(linLayout,linLayoutParam);             的LayoutParams lpView =新的LayoutParams(0,LayoutParams.WRAP_CONTENT);
             lpView.weight = 1;             的for(int i = 0; I< names.length;我++){             按钮BTN =新按钮(本);
             btn.setText(名称[I]);
             linLayout.addView(BTN,lpView);
             }
             返回true;
     }赶上(例外前){
         返回false;
     }}

I want to create a function that takes in an array of text and creates buttons and adds them to the view.

This is my code.

It is working and creating the buttons but when i call the function twice it doesn't create two linear layouts it just shows the last one called as if it is deleting the first one.

How can i make it to create a new linear layout and add it to the View?

// Create a view 
protected boolean CreateTheButtons(String[] names)
{
    try
    {
             LinearLayout linLayout = new LinearLayout(this);
             linLayout.setOrientation(LinearLayout.HORIZONTAL);
             LayoutParams linLayoutParam = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
             // set LinearLayout as a root element of the screen 
             linLayout.setWeightSum(names.length);
             setContentView(linLayout, linLayoutParam);

             LayoutParams lpView = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
             lpView.weight = 1;

             for (int i = 0; i < names.length; i++) {

             Button btn = new Button(this);
             btn.setText(names[i]);
             linLayout.addView(btn, lpView);

             }
             return true;
     }
     catch(Exception ex)
     {

         return false;
     }




}

解决方案

It is working and creating the buttons but when i call the function twice it doesn't create two linear layouts it just shows the last one called as if it is deleting the first one.

Your code removes the first LinearLayout resulted from calling the method because you use setContentView()(which will replace the current view of the activity(if any is found) with the view that you pass as a parameter). Instead you should remove the call to setContentView() and insert a holder ViewGroup for the LinearLayouts that you plan to add through that method.

<!-- This will be the content view of the activity -->
<?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:orientation="vertical"
    android:id="@+id/parent" />

Set the layout above as the content view for the activity, in the onCreate() method:

setContentView(R.layout.the_layout_above);

In the method you'll then have:

protected boolean CreateTheButtons(String[] names) {
    try {
             LinearLayout linLayout = new LinearLayout(this);
             linLayout.setOrientation(LinearLayout.HORIZONTAL);
             LinearLayout.LayoutParams linLayoutParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);              
             linLayout.setWeightSum(names.length);
             // assuming this method is in an Activity
             LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
             parent.addView(linLayout, linLayoutParam);

             LayoutParams lpView = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
             lpView.weight = 1;

             for (int i = 0; i < names.length; i++) {

             Button btn = new Button(this);
             btn.setText(names[i]);
             linLayout.addView(btn, lpView);
             }
             return true;
     } catch(Exception ex) {
         return false;
     }

}

这篇关于动态地创建视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆