如何在单击按钮时膨胀另一个 xml

发布于 2024-11-14 18:31:06 字数 1366 浏览 3 评论 0原文

我有一个 Activity ,因为没有 Button ,但我已经以这样的方式对其进行了编码:另一个 Activity 出现,现在就在这一秒Activity 有三个 Button,当我单击所有按钮时,会展开不同的相应活动。 在第二个 Activity 中,我又添加了一个 Button,单击此第四个 Button 后,我不需要另一个 Activity code> 进入前台,而不是我只想膨胀另一个布局(我不想更改 Activity) 在该布局(我需要膨胀)中,我有一个 ListView ,其中包含某些站点的列表,当此布局膨胀时,应显示站点列表,当我单击该站点时,它应该带我去那个网页。

问题 1: 如何在单击第四个 Button 时仅膨胀 xml 而不更改 Activity

问题2:xml 膨胀时,如果我单击 BackButton,则应显示包含所有 4 个 ButtonActivity

这是我用来膨胀另一个 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)callingMore() 方法中 xml 被夸大了,但是 xml 没有显示任何内容,然后如果我点击 BackButtonActivity未显示全部 4 个 Button

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生来就爱笑 2024-11-21 18:31:06

我尝试了这段代码..希望对你有帮助..

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

            }
        });
    }
}

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

            }
        });
    }
}
孤独难免 2024-11-21 18:31:06

尝试使用 v.findViewByID()..

将 View v 传递给 CallingMore()

Try using v.findViewByID()..

Pass View v to callingMore()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文