【问题标题】:How to create a button dynamically?如何动态创建按钮?
【发布时间】:2012-05-11 19:35:30
【问题描述】:

我知道很多人都问过这个问题,但我的问题有所不同:

我已经在 xml 中有一个布局(使用选项卡),但我需要根据字符串的读取来创建按钮。

例如: 查询返回四个单词。我需要创建四个按钮,点击时会出现带有单词的 toast。

注意:我不知道会找到多少字。

代码如下所示:

    while (Content.indexOf("<glossary>") != -1) {
        Content = Content.substring(Content.indexOf("<glossary>") + 9);
        //create button with name "Content"
    }

编辑:

看看我创建一个按钮的新代码:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showphyto);
    intent = getIntent();
    Title = intent.getStringExtra("Title");
    Content = intent.getStringExtra("Content");
    setTitle(Title);
    //if(getIntent().getData().getQueryParameter("author") != null)
    TabSpec descritor;

    /************************** Tab General **************************/
    descritor = getTabHost().newTabSpec("tag1");
    descritor.setContent(R.id.General);

    Button myButton = new Button(ShowAllegationActivity.this);
    myButton.setText("test");
    LinearLayout myContainer = (LinearLayout) findViewById(R.id.General);
    myContainer.addView(myButton);

    descritor.setIndicator("General", getResources().getDrawable(R.drawable.icon));
    getTabHost().addTab(descritor);

    /************************** Tab 2 **************************/
    descritor = getTabHost().newTabSpec("tag2");
    descritor.setContent(R.id.Recipe);
    Teste = (TextView) findViewById(R.id.teste2);
    Teste.setText("Teste2");
    descritor.setIndicator("Tab2", getResources().getDrawable(R.drawable.icon));
    getTabHost().addTab(descritor);

    /************************** Tab3 3 **************************/
    descritor = getTabHost().newTabSpec("tag3");
    descritor.setContent(R.id.Related);
    Teste = (TextView) findViewById(R.id.teste3);
    Teste.setText("Teste3");
    descritor.setIndicator("Tab3", getResources().getDrawable(R.drawable.icon));
    getTabHost().addTab(descritor);

    getTabHost().setCurrentTab(0);

}

}

xml 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabHost android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical">
        <TabWidget android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@android:id/tabs"></TabWidget>
        <FrameLayout android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:id="@android:id/tabcontent">
            <LinearLayout android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:id="@+id/General">
                    <TextView android:layout_width="fill_parent"
                    android:layout_height="wrap_content" 
                    android:id="@+id/teste" />
                    <LinearLayout android:layout_width="fill_parent" 
                    android:layout_height="fill_parent" 
                    android:id="@+id/General2">
                    </LinearLayout>
            </LinearLayout>
            <LinearLayout android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:id="@+id/Recipe">
                    <TextView android:layout_width="fill_parent"
                    android:layout_height="wrap_content" 
                    android:id="@+id/teste2" />
            </LinearLayout>
            <LinearLayout android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:id="@+id/Related">
                    <TextView android:layout_width="fill_parent"
                    android:layout_height="wrap_content" 
                    android:id="@+id/teste3" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

【问题讨论】:

    标签: android android-layout android-button


    【解决方案1】:

    您可以在代码中创建一个按钮:

    Button myButton = new Button();
    myButton.setLayoutParams(myLayoutParams);
    myButton.setText(myText);
    

    要让它显示 Toast,添加一个 onClickListener:

    myButton.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View view)
                        {
                            //The parameter view is the button that was clicked 
                            Toast.makeText(context, ((Button) view).getText(), Toast.LENGTH_LONG).show()
                            //TypeCasting to Button is important, because a normal View doesn't have a getText method
                        }
                    });
    

    然后,要将它们添加到您的屏幕,您需要在 xml 中定义一个容器来保存它们,例如 LinearLayout。使用以下方法获取 LinearLayout:

    LinearLayout myContainer = findViewById(R.id.myButtonId); //Make sure you set the android:id attribute in xml
    

    您可能希望这段代码在循环之外,可能就在循环之前,这样您就不会在每次迭代时都获得布局。

    然后,在循环中,设置按钮后:

    myContainer.addView(myButton);
    

    最后,关于myLayoutParams 变量的注释。当您为视图设置参数时,它们需要与您的小部件的父视图相对应。因此,如果您将 Button 放在 LinearLayout 中,它看起来像

    //This will make LayoutParameters with layout_width="wrap_content" and layout_height="fill_parent"
    LinearLayout.LayoutParams myLayoutParams = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT, 
        LinearLayout.LayoutParams.FILL_PARENT);
    

    您也可以将它放在循环之外,并为每个按钮重复使用它。

    【讨论】:

    • 您好,马特,感谢您的回复。我不能,我用完整的代码编辑了帖子,你能告诉我有什么问题吗?谢谢!
    • 我需要在 xml 中创建一个 Button?
    • 您不应该需要 xml 中的按钮。目前出了什么问题?程序是崩溃了,还是没有按您的预期工作?如果它崩溃,请发布您的 logcat 以获取错误。如果没有,查看“showphyto”xml 文件可能会很有用。由于看起来您正在使用选项卡布局,因此来自 google 的本教程也可能会提供一些帮助 developer.android.com/resources/tutorials/views/… 选项卡不像普通按钮那样设置,这可能是您的问题的一部分。
    • 只是没有达到我的预期!我用 xml 文件进行了编辑。请看。
    • 查看我在之前评论中发布的链接上的#4-6。看起来,而不是按钮,您使用 TabHost 来加载一堆触发意图的选项卡。选项卡位于 TabWidget 内,并且 Intent 是在 FrameLayout 中创建的。我以前从未使用过这种特定的布局,但是按照#6 中设置选项卡的方式设置“按钮”可能会有所帮助。你希望你的标签在点击后做什么?如果只是启动 Toast,则最好使用不太复杂的布局。
    【解决方案2】:

    你可以做的是这样的:

        Button button = new Button(this);
        ViewGroup viewGroup = (ViewGroup) findViewById(R.id.container);
    
        viewGroup.addView(button);
    

    我不知道你的布局,所以我不能更具体。

    ViewGroup 将是您的布局,它的 id 应该由 findByViewId 找到。

    此外,您还必须按照自己的意愿放置按钮。

    【讨论】:

      【解决方案3】:
       while (Content.indexOf("<glossary>") != -1) {
          Content = Content.substring(Content.indexOf("<glossary>") + 9);
          //create button with name "Content"
          Button b = new Button(this);
          b.setText(Content);
          // add this new view to your parent layout
          parent.addView(b);
      
      }
      

      【讨论】:

        【解决方案4】:

        您需要在 XML 中包含一个布局来保存您添加的按钮(您可以动态执行此操作,但似乎没有必要)。当你想添加一个按钮时,找到那个布局。然后您可以使用ViewGroup.addView(View child) 将您的按钮添加到视图层次结构中。

        您还需要告诉系统布局已更改,您可以使用 View.invalidate() 方法来做到这一点。

        【讨论】:

          【解决方案5】:

          我改变了创建选项卡的方式,它奏效了!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-02-17
            • 2014-11-08
            • 2011-10-22
            • 2013-09-05
            • 2019-07-26
            • 1970-01-01
            • 2023-03-28
            • 1970-01-01
            相关资源
            最近更新 更多