Android 用Fragment建立一個選項卡,androidfragment

來源:互聯網
上載者:User

Android 用Fragment建立一個選項卡,androidfragment

本文結合之前的動態建立fragment來進行一個實踐,來實現用Fragment建立一個選項卡

本文地址:http://www.cnblogs.com/wuyudong/p/5898075.html ,轉載請註明源地址。

項目布局

<LinearLayout 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"    android:orientation="vertical"    tools:context=".MainActivity" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:id="@+id/tab1"            android:layout_width="0dip"            android:layout_height="wrap_content"            android:layout_weight="1"            android:gravity="center"            android:text="社會新聞" />        <TextView            android:id="@+id/tab2"            android:layout_width="0dip"            android:layout_height="wrap_content"            android:layout_weight="1"            android:gravity="center"            android:text="生活新聞" />        <TextView            android:id="@+id/tab3"            android:layout_width="0dip"            android:layout_height="wrap_content"            android:layout_weight="1"            android:gravity="center"            android:text="軍事新聞" />        <TextView            android:id="@+id/tab4"            android:layout_width="0dip"            android:layout_height="wrap_content"            android:layout_weight="1"            android:gravity="center"            android:text="娛樂新聞" />    </LinearLayout>    <LinearLayout        android:id="@+id/content"        android:layout_width="fill_parent"        android:layout_height="fill_parent" >    </LinearLayout></LinearLayout>

建立Fragment1.java~Fragment4.java,其中Fragment1.java中的代碼如下:

public class Fragment1 extends Fragment {    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment1, null);    }}

其他幾個檔案的代碼類似

建立fragment1.xml~fragment4.xml,其中fragment1.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:gravity="center"    android:orientation="vertical" >    <TextView        android:id="@+id/textview1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="社會新聞"         android:textAppearance="?android:attr/textAppearanceLarge"/></LinearLayout>

其他幾個檔案的代碼類似

MainActivity.java中的代碼如下:

public class MainActivity extends Activity implements OnClickListener {    private LinearLayout content;    private TextView tv1, tv2, tv3, tv4;    private FragmentManager fm;    private FragmentTransaction ft;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        content = (LinearLayout) findViewById(R.id.content);        tv1 = (TextView) findViewById(R.id.tab1);        tv2 = (TextView) findViewById(R.id.tab2);        tv3 = (TextView) findViewById(R.id.tab3);        tv4 = (TextView) findViewById(R.id.tab4);        tv1.setOnClickListener(this);        tv2.setOnClickListener(this);        tv3.setOnClickListener(this);        tv4.setOnClickListener(this);        fm = getFragmentManager();        ft = fm.beginTransaction();        ft.replace(R.id.content, new Fragment1()); // 預設情況下Fragment1    }    @Override    public void onClick(View v) {        ft = fm.beginTransaction();        switch (v.getId()) {        case R.id.tab1:            ft.replace(R.id.content, new Fragment1());            break;        case R.id.tab2:            ft.replace(R.id.content, new Fragment2());            break;        case R.id.tab3:            ft.replace(R.id.content, new Fragment3());            break;        case R.id.tab4:            ft.replace(R.id.content, new Fragment4());            break;        default:            break;        }        ft.commit();    }}

運行項目後如下效果:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.