我目前正在開發Android項目,我需要在同一活動中使用多個佈局。在單個活動中使用多個佈局
當用戶點擊主佈局中的一個按鈕時,我需要另一個佈局(這是完全不同的)與不同的內容。
我試着在互聯網上查找,發現可以使用碎片,但據我瞭解,碎片將用於新佈局只需要部分更改時使用碎片,而我需要使用完全不同的碎片佈局。
此外,我發現包括,但是這是在多個活動中使用相同的佈局。 所以,不是我在找什麼。
有沒有人有任何想法如何做到這一點?
我目前正在開發Android項目,我需要在同一活動中使用多個佈局。在單個活動中使用多個佈局
當用戶點擊主佈局中的一個按鈕時,我需要另一個佈局(這是完全不同的)與不同的內容。
我試着在互聯網上查找,發現可以使用碎片,但據我瞭解,碎片將用於新佈局只需要部分更改時使用碎片,而我需要使用完全不同的碎片佈局。
此外,我發現包括,但是這是在多個活動中使用相同的佈局。 所以,不是我在找什麼。
有沒有人有任何想法如何做到這一點?
嘗試使用fragments
您可以在一個活動顯示,許多片段
我不這麼認爲片段只能用於局部修改使用,希望它與動態視圖以及良好
http://developer.android.com/guide/components/fragments.html
XML使用framelayout。把你的內容,我剛纔提到< - 您的佈局 - >
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/Layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<--Your layout-->
<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:id="@+id/Layout2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<--Your layout-->
</LinearLayout>
</FrameLayout>
代碼:的onclick設置按鈕,設置的知名度。
LinearLayout 1ayout1,layout2;
Button button1;
1ayout1 = (LinearLayout) findViewById(R.id.Layout1);
1ayout2 = (LinearLayout) findViewById(R.id.Layout2);
button1=(Button)findViewById(R.id.Button1);
button1.setOnClickListener(this);
1ayout1.setVisibility(LinearLayout.VISIBLE);
1ayout2.setVisibility(LinearLayout.GONE);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
1ayout2.setVisibility(LinearLayout.VISIBLE);
1ayout1.setVisibility(LinearLayout.GONE);
}
}
當他按下按鈕時,爲什麼不用所需的佈局打開一個新的活動? – TomerZ 2014-10-26 15:24:28
使用視圖分頁器。然後按鈕上點擊只是改變佈局..刪除舊的佈局,並添加新的佈局。 – Meenal 2014-10-26 15:29:50
你可以做setContentView(R.layout.your_new_layout); – 2014-10-26 15:30:16