简体   繁体   English

在android上动态创建按钮

[英]Creating buttons dynamically on android

i'm trying to create buttons dynamically though code using a linear layout to set one button after the other, my code runs doesn't give any errors, nor throwing any exceptions or anything, but all i get is an empty screen. 我试图通过代码使用线性布局设置一个按钮动态创建按钮,我的代码运行不会给出任何错误,也不会抛出任何异常或任何东西,但我得到的只是一个空屏幕。 Thanks in advance. 提前致谢。

private void runThreadCreateButton(final List<Stop> stops) {

    new Thread() {
        public void run() {
            int i=0;
            while (i++ < 1) {
                try {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            for(int j=0;j<stops.size();j++)
                            {
                                Button myButton = new Button(getApplicationContext());
                                myButton.setText(stops.get(j).getName());

                                LinearLayout ll = new LinearLayout(getApplicationContext());

                                LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                                ll.addView(myButton, lp);
                            }
                        }
                    });
                    Thread.sleep(1000);
                } catch (Exception e) {
                   System.out.println(e.getMessage());
                }
            }
        }
    }.start();
}

you create a new Layout, but you Activity is not set to show your layout. 您创建一个新的布局,但您没有设置Activity来显示您的布局。 Call setContentView() on your Main Activity with your new layout. 使用新布局调用Main Activity上的setContentView()

LinearLayout ll = new LinearLayout(getApplicationContext());
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
//add something like this
yourMainActivity.setContentView(ll);

Well, if you didn't add your layout to your activity this was your main problem. 好吧,如果你没有将你的布局添加到你的活动,这是你的主要问题。

However you should learn about composing your layouts in xml. 但是,您应该了解如何在xml中编写布局。 This makes your code a lot more readable and maintainable. 这使您的代码更具可读性和可维护性。 Also you don't need to worry about switching Threads all the time. 此外,您无需担心始终切换线程。

If you need to populate your views with runtime data, you should use list views and list adapters. 如果需要使用运行时数据填充视图,则应使用列表视图和列表适配器。 ListViews and ListAdapters are essentials to almost every android app, so you you should really learn about them. ListViews和ListAdapters是几乎所有Android应用程序的基本要素,所以你应该真正了解它们。 If you want your list items to hold more complex layout you can do so by implementing your own custom list adapters. 如果希望列表项包含更复杂的布局,可以通过实现自己的自定义列表适配器来实现。

There are also lots of performance tweaks you can do, when using list views. 使用列表视图时,您还可以进行大量性能调整。 For most use cases generating and managing your layout from code is not a good approach. 对于大多数用例,从代码生成和管理布局不是一个好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM