2014年7月15日 星期二

套用現成的Tab樣式並在Fagment間換頁

套用現成的Tab樣式
 (**只有選HOLO不會出錯)
download file->go elipse ->copy drawable..etc 取代舊的->copy style 到 value/style , 把style name=改成"AppBaseTheme" -->done. 


http://jgilfelt.github.io/android-actionbarstylegenerator/#name=example&compat=holo&theme=light&actionbarstyle=solid&texture=0&hairline=0&neutralPressed=1&backColor=f44%2C100&secondaryColor=fbb2b2%2C100&tabColor=000%2C100&tertiaryColor=fbb2b2%2C100&accentColor=f44%2C100&cabBackColor=fff%2C100&cabHighlightColor=33b5e5%2C100

src/java

FragmentA

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.fragment_a, container, false);
return rootView;
}

}

 FragmentB

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentB extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView=inflater.inflate(R.layout.fragment_b, container,false);
return rootView;
}


}

 FragmentC

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentC extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView=inflater.inflate(R.layout.fragment_c, container,false);
return rootView;
}

}



MainActivity

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar=getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab=actionBar.newTab()
.setText("First Tab")
.setTabListener(new AllTabListener<FragmentA>(this,"fragmentA",FragmentA.class));
actionBar.addTab(tab);
tab=actionBar.newTab()
.setText("Second Tab")
.setTabListener(new AllTabListener<FragmentB>(this,"fragmentB",FragmentB.class));
actionBar.addTab(tab);
tab=actionBar.newTab()
.setText("Third Tab")
.setTabListener(new AllTabListener<FragmentC>(this,"fragmentC",FragmentC.class));
actionBar.addTab(tab);
}
public static class AllTabListener<T extends Fragment> implements ActionBar.TabListener{
private final Class<T> mClass;
private final Activity mActivity;
private final String mTag;
private  T mFragment;
public AllTabListener(Activity activity,String tag,Class<T> clz){
mActivity=activity;
mClass=clz;
mTag=tag;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(mFragment==null){
mFragment=(T)T.instantiate(mActivity,mClass.getName());
ft.add(R.id.viewContainer, mFragment,mTag);
}else{
ft.attach(mFragment);
}
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if(mFragment!=null){
ft.detach(mFragment);
}
}
}
}

res/layout
(**沒有activity_main)

FragmentA

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:text="FragmentA" >

</LinearLayout>

FragmentB

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:text="FragmentB" >

</LinearLayout>

FragmentC

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:text="FragmentC" >

</LinearLayout>

Main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/viewContainer">"
    

</LinearLayout>


沒有留言:

張貼留言