1

我有一个Activity没有Buttons 但我已经将它编码为另一个Activity来的方式,现在在这一秒Activity有三个Buttons,当我单击所有膨胀不同的相应活动时。在这一秒Activity中,我又添加了一个Button,在单击第四个时,Button我不希望另一个Activity出现在前台,而是我只想让另一个布局膨胀(我不想更改Activity)在那个布局中(我需要膨胀)我有一个ListView包含某些网站的列表,当这个布局膨胀时,应该显示网站列表,当我点击那里的网站时,它应该把我带到那个网页。

问题 1: 如何在xmlButton改变Activity.

问题 2:xml充气时,如果我点击BackButton包含Activity所有 4 Buttons 应该显示。

这是我用来给另一个充气的代码xml

fourthButton.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        callingMore();
    }
});
private void callingMore() {
    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.web_site_list_xml, (ViewGroup)findViewById(R.id.site_list));
}

但是这段代码给出了一个关闭的力并且没有xml被充气,如果我使用setContentView( R.layout.web_site_list_xml)inside callingMore()方法,它xml被充气但xml没有显示任何东西,然后如果我点击BackButton,则没有显示Activity所有 4 Buttons。

4

2 回答 2

6

我试过这个代码..希望对你有帮助..

1.layout_existed.xml

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/existedlayout"
    >
   <Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Clicck to inflate view"
    android:id="@+id/button"
    />
</LinearLayout>   

2.layout_toinfliate.xml

<?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">
  <TextView
  android:id="@+id/mytext"
  android:text="You inflated me..i added to u r layout"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />
</LinearLayout>

3.LayoutInflateDemo.java

  public class LayoutInflateDemo extends Activity {

    private LinearLayout layoutToAdd;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_existed);
        layoutToAdd = (LinearLayout) findViewById(R.id.existedlayout);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                LayoutInflater inflater = LayoutInflater
                        .from(getApplicationContext());
                View view = inflater.inflate(R.layout.layout_toinfliate, null);
                layoutToAdd.addView(view);

            }
        });
    }
}
于 2011-06-09T09:27:16.257 回答
-1

尝试使用 v.findViewByID()..

将 View v 传递给 callMore()

于 2011-06-09T08:16:17.697 回答