Android实现底部菜单Tab

以前在我的博客里面写过模仿网易新闻客户端顶部菜单Tab的文章

http://blog.csdn.net/shineflowers/article/details/41349703

现在使用静态的Fragment来实现底部Tab的功能:

<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"  
     >  
    <fragment  
        android:id="@+id/fragment"  
        android:name="com.example.fragment.ContentFragment"    
        android:layout_width="fill_parent"  
        android:layout_height="300dp"  
         />  
    <LinearLayout   
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:orientation="horizontal"  
        >  
        <Button   
            android:id="@+id/btn_news"  
            android:layout_width="0dp"  
            android:layout_height="wrap_content"  
            android:text="新闻"  
            android:layout_weight="1"  
            />  
        <Button   
            android:id="@+id/btn_entertainment"  
            android:layout_width="0dp"  
            android:layout_height="wrap_content"  
            android:text="娱乐"  
            android:layout_weight="1"  
            />  
        <Button   
            android:id="@+id/btn_political"  
            android:layout_width="0dp"  
            android:layout_height="wrap_content"  
            android:text="政治"  
            android:layout_weight="1"  
            />  
        <Button   
            android:id="@+id/btn_sports"  
            android:layout_width="0dp"  
            android:layout_height="wrap_content"  
            android:text="体育"  
            android:layout_weight="1"  
            />  
    </LinearLayout>  
</LinearLayout>

要实现的功能就是点击下面四个按钮,内容进行切换:

ContentFragment.java

public class ContentFragment extends Fragment {
    private TextView tv_content;
    private View rootView;
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_news, null);
        tv_content = (TextView) rootView.findViewById(R.id.tv_content);
        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    public void  setContentMsg(String msg){
        tv_content.setText(msg);
    }
}

MainActivity.java

public class MainActivity extends FragmentActivity implements OnClickListener {
private Button btn_news;
private Button btn_entertainment;
private Button btn_political;
private Button btn_sports;
private ContentFragment contentFragment;
    FragmentTransaction transaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btn_news = (Button) findViewById(R.id.btn_news);
        btn_entertainment = (Button) findViewById(R.id.btn_entertainment);
        btn_political = (Button) findViewById(R.id.btn_political);
        btn_sports = (Button) findViewById(R.id.btn_sports);
        
        
        FragmentManager fragmentManager = getSupportFragmentManager();
        contentFragment = (NewFragMent) fragmentManager.findFragmentById(R.id.fragment);
        contentFragment.setContentMsg("新闻");
        
        btn_news.setOnClickListener(this);
        btn_entertainment.setOnClickListener(this);
        btn_political.setOnClickListener(this);
        btn_sports.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_news:
                contentFragment.setContentMsg("新闻");
                break;
            case R.id.btn_entertainment:
                contentFragment.setContentMsg("娱乐");
                break;
            case R.id.btn_political:
                contentFragment.setContentMsg("政治");
                break;
            case R.id.btn_sports:
                contentFragment.setContentMsg("体育");
                break;
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值