使用Fragment實現動態布局

來源:互聯網
上載者:User

MainActivity:

package com.home.testfragment;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTransaction;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends FragmentActivity implements OnClickListener {private Button addFirstFragmentBtn;private Button replaceFirstFragmentBtn;private Button removeFirstFragmentBtn;private FirstFragment firstFragment;@Overrideprotected void onCreate(Bundle arg0) {super.onCreate(arg0);setContentView(R.layout.main);addFirstFragmentBtn = (Button) findViewById(R.id.main_btn_add);replaceFirstFragmentBtn = (Button) findViewById(R.id.main_btn_replace);removeFirstFragmentBtn = (Button) findViewById(R.id.main_btn_remove);addFirstFragmentBtn.setOnClickListener(this);replaceFirstFragmentBtn.setOnClickListener(this);removeFirstFragmentBtn.setOnClickListener(this);addFirstFragmentBtn.setEnabled(true);removeFirstFragmentBtn.setEnabled(false);}@Overridepublic void onClick(View v) {if (v == addFirstFragmentBtn) {addFirstFragmentBtn.setEnabled(false);removeFirstFragmentBtn.setEnabled(true);if (findViewById(R.id.main_container) != null) {firstFragment = new FirstFragment();FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();transaction.add(R.id.main_container, firstFragment);// 把當前Fragment添加至回退棧,通過返回鍵返回時可以導航到上一個Fragment狀態transaction.addToBackStack(null);// 提交transaction.commit();}}if (v == replaceFirstFragmentBtn) {addFirstFragmentBtn.setEnabled(true);removeFirstFragmentBtn.setEnabled(false);SecondFragment secondFragment = new SecondFragment();FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();transaction.replace(R.id.main_container, secondFragment);transaction.addToBackStack(null);transaction.commit();}if (v == removeFirstFragmentBtn) {addFirstFragmentBtn.setEnabled(true);removeFirstFragmentBtn.setEnabled(false);FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();transaction.remove(firstFragment);// 如果移除的時候也添加到回退棧,表示當前Fragment只是被停止而沒有被摧毀,返回時它將恢複;// 那麼如果不添加到回退棧則表示完全摧毀// transaction.addToBackStack(null);transaction.commit();}}}

FirstFragment:

package com.home.testfragment;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class FirstFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_first, container, false);}}

SecondFragment:

package com.home.testfragment;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class SecondFragment extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_second, container, false);}}

main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <FrameLayout        android:id="@+id/main_container"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:orientation="horizontal" >        <Button            android:id="@+id/main_btn_add"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="添加" />        <Button            android:id="@+id/main_btn_replace"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="替換" />        <Button            android:id="@+id/main_btn_remove"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="移除" />    </LinearLayout></RelativeLayout>


fragment_first.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"    android:orientation="vertical" >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="MyFirstFragment"        android:textSize="20sp" /></LinearLayout>

fragment_second.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"    android:orientation="vertical" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="MySecondFragment"        android:textSize="20sp" /></LinearLayout>






 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.