我想为每个片段添加自定义标题,它位于viewPager
. 我该如何实施。在下面的代码中,我需要为和FragmentMain
添加自定义标题。FragmentMore
FragmentChatView
提前致谢。
public class HomeActivity extends FragmentActivity {
// page adapter between fragment list and view pager
public static PagerAdapter mPagerAdapter;
// view pager
public ViewPager mPager;
// activity data
public String p2text, p3text;
public static List<Fragment> fragments;// = buildFragments();
// / ArrayList<String> categories = {"1","2","3","4","5","6","7","8"};
ArrayList<String> categories = new ArrayList<String>();
static final String LOG_TAG = "HomeActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragments = new ArrayList<android.support.v4.app.Fragment>();
categories.add("1");
categories.add("2");
categories.add("3");
categories.add("4");
categories.add("5");
categories.add("6");
categories.add("7");
addFragments(FragmentMore.class.getName(), 1);
addFragments(FragmentMain.class.getName(), 2);
addFragments(FragmentChatView.class.getName(), 3);
mPager = (ViewPager) super.findViewById(R.id.pager);
mPagerAdapter = new PagerAdapter(this, getSupportFragmentManager(), fragments, categories);
mPager.setAdapter(mPagerAdapter);
mPager.setCurrentItem(1);
}
public void addFragments(String className, int position) {
// List<android.support.v4.app.Fragment> fragments = new
// ArrayList<android.support.v4.app.Fragment>();
// (int i = 0; i<categories.size(); i++) {
Bundle b = new Bundle();
b.putInt("position", position);
fragments.add(Fragment.instantiate(this, className, b));
// }
// return fragments;
}
public void removeFragments(String className, int position) {
// List<android.support.v4.app.Fragment> fragments = new
// ArrayList<android.support.v4.app.Fragment>();
// (int i = 0; i<categories.size(); i++) {
Bundle b = new Bundle();
b.putInt("position", position);
fragments.remove(Fragment.instantiate(this, className, b));
// }
// return fragments;
}
private List<android.support.v4.app.Fragment> buildFragments() {
List<android.support.v4.app.Fragment> fragments = new ArrayList<android.support.v4.app.Fragment>();
for (int i = 0; i < categories.size(); i++) {
Bundle b = new Bundle();
b.putInt("position", i);
fragments.add(Fragment.instantiate(this, FragmentSearch.class.getName(), b));
}
return fragments;
}
@Override
public void onResume() {
super.onResume();
Log.e(LOG_TAG, "onResume");
}
@Override
public void onPause() {
super.onPause();
Log.e(LOG_TAG, "onPause");
}
}
public class FragmentMore extends Fragment {
Button btnWrite;
public String ptext="More";
static final String LOG_TAG = "Fragment More";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
// onCreateView :
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.more,container,false);
return view;
}
@Override
public void onResume() {
super.onResume();
Log.e(LOG_TAG, "onResume");
}
@Override
public void onPause() {
super.onPause();
Log.e(LOG_TAG, "onPause");
}
}
//更多布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/screen_back_color"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<Button
android:id="@+id/btnSettings"
android:layout_width="@dimen/more_button_width"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/more_button_margin"
android:layout_marginTop="@dimen/more_button_margin"
android:background="@drawable/button_selector_grey"
android:text="@string/settings"
android:textAllCaps="true"
android:textSize="@dimen/more_button_font_size"
android:textColor="@color/white" >
</Button>
<Button
android:id="@+id/btnTermsAndConditions"
android:layout_width="@dimen/more_button_width"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/more_button_margin"
android:layout_marginTop="@dimen/more_button_margin"
android:background="@drawable/button_selector_grey"
android:text="@string/termsandCondition"
android:textAllCaps="true"
android:textColor="@color/white" >
</Button>
</LinearLayout>
//自定义标题布局
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="fill_horizontal"
android:background="@color/black"
android:orientation="horizontal" >
<TextView
android:id="@+id/header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="@dimen/main_edittext_fontsize"
android:contentDescription="@string/desc"
android:gravity="center"
android:textColor="@color/light_blue"
android:paddingBottom="7dp"
android:paddingTop="7dp"
android:text="title" />
</RelativeLayout>