Fragment应用,及创建选项卡

xml:

<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="com.example.fragment123.MainActivity"
    >
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        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="选项1"
            />
        <TextView 
            android:id="@+id/tab2"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="选项2"
            />
        <TextView 
            android:id="@+id/tab3"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="选项3"
            />
        <TextView 
            android:id="@+id/tab4"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="选项4"
            />
    </LinearLayout>


    <LinearLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    </LinearLayout>
   
</LinearLayout>

代码:

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;




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());
ft.commit();


}


@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();
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值