public class MainActivity extends Activity {
FragmentManager fragmentManager;
FragmentTransaction transaction;
ImageView[] image = new ImageView[4];
TextView[] text = new TextView[4];
Fragment[] frag = new Fragment[4];
int[] selectedId ={R.drawable.message_selected, R.drawable.contacts_selected,
R.drawable.news_selected, R.drawable.setting_selected};
int[] unselectedId ={R.drawable.message_unselected, R.drawable.contacts_unselected,
R.drawable.news_unselected, R.drawable.setting_unselected};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
frag[0] = new MessageFragment();
frag[1] = new ContactsFragment();
frag[2] = new NewsFragment();
frag[3] = new SettingFragment();
image[0] = (ImageView) findViewById(R.id.message_image);
image[1] = (ImageView) findViewById(R.id.contacts_image);
image[2] = (ImageView) findViewById(R.id.news_image);
image[3] = (ImageView) findViewById(R.id.setting_image);
text[0] = (TextView) findViewById(R.id.message_text);
text[1] = (TextView) findViewById(R.id.contacts_text);
text[2] = (TextView) findViewById(R.id.news_text);
text[3] = (TextView) findViewById(R.id.setting_text);
fragmentManager = getFragmentManager();
transaction = fragmentManager.beginTransaction();
for(int i = 0; i < frag.length; i++){
transaction.add(R.id.FrameLayoutId, frag[i]);
}
transaction.commit();
setTabSelection(0);
}
public void onClickBtn(View v) {
switch (v.getId()) {
case R.id.message_layout_btn:
setTabSelection(0);
break;
case R.id.contacts_layout_btn:
setTabSelection(1);
break;
case R.id.news_layout_btn:
setTabSelection(2);
break;
case R.id.setting_layout_btn:
setTabSelection(3);
break;
default:
break;
}
}
private void setTabSelection(int index) {
transaction = fragmentManager.beginTransaction();
unsetTabSelection(0);
unsetTabSelection(1);
unsetTabSelection(2);
unsetTabSelection(3);
image[index].setImageResource(selectedId[index]);
text[index].setTextColor(Color.WHITE);
transaction.show(frag[index]);
transaction.commit();
}
private void unsetTabSelection(int index) {
image[index].setImageResource(selectedId[index]);
text[index].setTextColor(Color.parseColor("#82858b"));
transaction.hide(frag[index]);
}
}
其中之一Fragment:
public class MessageFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View messageLayout = inflater.inflate(R.layout.message_layout,
container, false);
return messageLayout;
}
}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/FrameLayoutId"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/tab_bg" >
<RelativeLayout
android:id="@+id/message_layout_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClickBtn" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
<ImageView
android:id="@+id/message_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/message_unselected" />
<TextView
android:id="@+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="消息"
android:textColor="#82858b" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/contacts_layout_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClickBtn" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
<ImageView
android:id="@+id/contacts_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/contacts_unselected" />
<TextView
android:id="@+id/contacts_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="联系人"
android:textColor="#82858b" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/news_layout_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClickBtn" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
<ImageView
android:id="@+id/news_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/news_unselected" />
<TextView
android:id="@+id/news_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="动态"
android:textColor="#82858b" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/setting_layout_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClickBtn" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
<ImageView
android:id="@+id/setting_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/setting_unselected" />
<TextView
android:id="@+id/setting_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="设置"
android:textColor="#82858b" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
其中之一xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/message_selected" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="消息界面"
android:textSize="20sp" />
</LinearLayout>
</RelativeLayout>
代码就是最好的注释,留着备用。