Android Studio活动布局更改

Android Studio活动布局更改,android,Android,当我点击按钮时,我需要帮助来改变Android Studio中活动的布局 但我不想打开新活动,我想通过单击按钮完全更改当前活动的布局和设计。有可能吗?是的,通过使用setContentView()方法可以实现。但您必须确保未使用当前布局上不可用的任何视图(例如,按id查看)。您可以在按钮期间充气另一个布局。单击: public class LayoutInflateDemo extends Activity { private LinearLayout layoutToAdd;

当我点击按钮时,我需要帮助来改变Android Studio中活动的布局


但我不想打开新活动,我想通过单击按钮完全更改当前活动的布局和设计。有可能吗?

是的,通过使用
setContentView()
方法可以实现。但您必须确保未使用当前布局上不可用的任何视图(例如,按id查看)。

您可以在
按钮期间充气另一个布局。
单击:

    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);
                //Add the inflated layout to your existing view 
                layoutToAdd.addView(view);

            }
        });
    }
}
用于膨胀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="Inflated layout"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />
</LinearLayout>

这是您现有的布局:

 <?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>   


最好的方法是片段片段活动

如果我在单个活动上有多个按钮,如何在布局之间切换?只需在多个按钮单击过程中膨胀不同的布局…并将它们添加到现有布局中我对android studio是全新的。抱歉问了个愚蠢的问题。但是膨胀Xml我将创建,还是它已经存在,我将添加此代码?layout_toinfliate是布局资源的id…您需要在单击按钮时显示它--您需要创建它并将代码添加到您的文件中