一般界面跳转,效果图如下:
实现代码:
package com.example.fragmentapp;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView messageText, contactText, startText;
private MessageFragment messageFragment;
private ContactFragment contactFragment;
private StartFragment startFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messageText = (TextView) findViewById(R.id.message);
contactText = (TextView) findViewById(R.id.contact);
startText = (TextView) findViewById(R.id.start);
messageFragment = new MessageFragment();
contactFragment = new ContactFragment();
startFragment = new StartFragment();
choose(0);
messageText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
choose(0);
}
});
contactText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
choose(1);
}
});
startText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
choose(2);
}
});
}
private void choose(int pos) {
if (pos == 0) {
messageText.setTextColor(Color.RED);
messageText.setBackgroundColor(Color.LTGRAY);
contactText.setTextColor(Color.DKGRAY);
contactText.setBackgroundColor(Color.WHITE);
startText.setTextColor(Color.DKGRAY);
startText.setBackgroundColor(Color.WHITE);
loadFragment(messageFragment);
}
if (pos == 1) {
contactText.setTextColor(Color.RED);
contactText.setBackgroundColor(Color.LTGRAY);
messageText.setTextColor(Color.DKGRAY);
messageText.setBackgroundColor(Color.WHITE);
startText.setTextColor(Color.DKGRAY);
startText.setBackgroundColor(Color.WHITE);
loadFragment(contactFragment);
}
if (pos == 2) {
startText.setTextColor(Color.RED);
startText.setBackgroundColor(Color.LTGRAY);
messageText.setTextColor(Color.DKGRAY);
messageText.setBackgroundColor(Color.WHITE);
contactText.setTextColor(Color.DKGRAY);
contactText.setBackgroundColor(Color.WHITE);
loadFragment(startFragment);
}
}
private void loadFragment(Fragment f) {
FragmentManager fm = this.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content, f);
ft.commit();
}
}
package com.example.fragmentapp;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MessageFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(android.R.layout.simple_list_item_1,null);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setText("消息界面");
text.setBackgroundColor(Color.YELLOW);
return view;
}
}
package com.example.fragmentapp;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ContactFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(android.R.layout.simple_list_item_1, null);
TextView text=(TextView) view.findViewById(android.R.id.text1);
text.setText("联系人界面");
text.setBackgroundColor(Color.GREEN);
return view;
}
}
package com.example.fragmentapp;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class StartFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(android.R.layout.simple_list_item_1, null);
TextView text=(TextView) view.findViewById(android.R.id.text1);
text.setText("动态界面");
text.setBackgroundColor(Color.RED);
return view;
}
}
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.fragmentapp.MainActivity" >
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="9" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textSize="25sp"
android:text="消息" />
<TextView
android:id="@+id/contact"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textSize="25sp"
android:text="联系人" />
<TextView
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textSize="25sp"
android:text="动态" />
</LinearLayout>
</LinearLayout>