以前在我的博客里面写过模仿网易新闻客户端顶部菜单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;
}
}