4

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

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

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

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

代码看起来像:

    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>

4

5 回答 5

4

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

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

您可能希望此代码位于循环之外,可能就在它之前,因此您不会在每次迭代时都获得 Layout。

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

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);

你也可以把它放在你的循环之外,并为每个按钮重用它。

于 2012-05-11T19:58:13.877 回答
2

你可以做的是这样的:

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

    viewGroup.addView(button);

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

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

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

于 2012-05-11T19:49:51.260 回答
1

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

于 2012-05-17T12:49:58.580 回答
1

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

您还需要告诉系统布局已更改,您可以使用 View.invalidate() 方法来完成。

于 2012-05-11T19:48:44.497 回答
1
 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);

}
于 2012-05-11T19:42:21.087 回答