How to inflate another xml on clicking a button

霸气de小男生 提交于 2019-12-02 08:07:05

问题


I have one Activity in that there are no Buttons but I have coded it in such a way that another Activity comes, now in this second Activity there are three Buttons, on which when I click all inflating different respective activities. In this second Activity there I have added one more Button, on clicking this 4th Button I don't want another Activity to come in foreground instead i just want another layout to be inflated(I don't wanna change the Activity) In that layout(which i need to inflate) I have a ListView which has a list of certain sites, when this layout is inflated the list of sites should be shown and as i click on there sites its should take me to that webpage.

PROBLEM 1: How to inflate just the xml on click of the 4th Button without changing the Activity.

PROBLEM 2: When the xml is inflated and if I click on BackButton the Activity containing all the 4 Buttons should be displayed.

Here is the code that I'm using to inflate another 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));
}

BUT this code gives a force close and no xml is being inflated and if I use setContentView( R.layout.web_site_list_xml) inside callingMore() method the xml is inflated but that xml doesn't show anything and then if I hit BackButton, the Activity that has all 4 Buttons isn't shown.


回答1:


I tried this code..hope helps to you..

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

            }
        });
    }
}



回答2:


Try using v.findViewByID()..

Pass View v to callingMore()



来源:https://stackoverflow.com/questions/6288973/how-to-inflate-another-xml-on-clicking-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!