配色: 字号:
Android实现底部菜单Tab
2017-01-05 | 阅:  转:  |  分享 
  
Android实现底部菜单Tab



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

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

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

[html]viewplaincopy在CODE上查看代码片派生到我的代码片


xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>


android:id="@+id/fragment"

android:name="com.example.fragment.ContentFragment"

android:layout_width="fill_parent"

android:layout_height="300dp"

/>


android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

>


android:id="@+id/btn_news"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:text="新闻"

android:layout_weight="1"

/>


android:id="@+id/btn_entertainment"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:text="娱乐"

android:layout_weight="1"

/>


android:id="@+id/btn_political"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:text="政治"

android:layout_weight="1"

/>


android:id="@+id/btn_sports"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:text="体育"

android:layout_weight="1"

/>





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

ContentFragment.Java

[java]viewplaincopy在CODE上查看代码片派生到我的代码片

publicclassContentFragmentextendsFragment{

privateTextViewtv_content;

privateViewrootView;

@Override

publicvoidonAttach(Activityactivity){

super.onAttach(activity);

}

@Override

publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,

BundlesavedInstanceState){

rootView=inflater.inflate(R.layout.activity_news,null);

tv_content=(TextView)rootView.findViewById(R.id.tv_content);

returnrootView;

}



@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

}

publicvoidsetContentMsg(Stringmsg){

tv_content.setText(msg);

}

}

MainActivity.java

[java]viewplaincopy在CODE上查看代码片派生到我的代码片

publicclassMainActivityextendsFragmentActivityimplementsOnClickListener{

privateButtonbtn_news;

privateButtonbtn_entertainment;

privateButtonbtn_political;

privateButtonbtn_sports;

privateContentFragmentcontentFragment;

FragmentTransactiontransaction;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

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);





FragmentManagerfragmentManager=getSupportFragmentManager();

contentFragment=(NewFragMent)fragmentManager.findFragmentById(R.id.fragment);

contentFragment.setCott951新闻(www.tt951.com)ntentMsg("



btn_news.setOnClickListener(this);

btn_entertainment.setOnClickListener(this);

btn_political.setOnClickListener(this);

btn_sports.setOnClickListener(this);

}

@Override

publicvoidonClick(Viewv){

switch(v.getId()){

caseR.id.btn_news:

contentFragment.setContentMsg("新闻");

break;

caseR.id.btn_entertainment:

contentFragment.setContentMsg("娱乐");

break;

caseR.id.btn_political:

contentFragment.setContentMsg("政治");

break;

caseR.id.btn_sports:

contentFragment.setContentMsg("体育");

break;

}

}

献花(0)
+1
(本文系thedust79首藏)