??今天我來說說ViewPager+tabPageIndicator來實(shí)現(xiàn)tab界面。首先tabPageIndicator是在第三方的library包里面-- viewPagerlibrary,在githup上可以下載,我們需要將它導(dǎo)入eclipse中,然后在關(guān)聯(lián)它。當(dāng)我們使用這個包的時候,有一個問題需要注意:可能主工程中會報錯,但是我們又不知道是哪里錯了,最好看看libs下的v4包,一定要保證主工程和library中的v4包要么一模一樣,要么只有一個,所以要么將其中一個v4包用另一個v4覆蓋,或者直接刪除一個包。
??好的,現(xiàn)在直接貼代碼
MainActivity布局文件代碼
<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.android_tab4.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include layout="@layout/top" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<com.viewpagerindicator.TabPageIndicator
android:id="@+id/TabPageIndicator"
android:background="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.viewpagerindicator.TabPageIndicator>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</LinearLayout>
MainActivity代碼
package com.example.android_tab4;
import com.example.Util.Myadapter;
import com.viewpagerindicator.TabPageIndicator;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Window;
public class MainActivity extends FragmentActivity {
private ViewPager viewpager = null;
private TabPageIndicator tabPageIndicator = null;
private Myadapter myadapter = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
}
private void initView()
{
viewpager = (ViewPager) findViewById(R.id.viewpager);
tabPageIndicator = (TabPageIndicator) findViewById(R.id.TabPageIndicator);
myadapter = new Myadapter(getSupportFragmentManager());
viewpager.setAdapter(myadapter);
tabPageIndicator.setViewPager(viewpager, 0);
}
}
MyFragment代碼
package com.example.Util;
import com.example.android_tab4.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyFragment extends Fragment{
String string = null;
public MyFragment(int position)
{
string = Myadapter.string[position];
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
TextView textview = (TextView) view.findViewById(R.id.textview);
textview.setText(string);
return view;
}
}
MyFragment布局文件代碼
<?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" >
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
/>
</LinearLayout>
MyAdapter代碼
package com.example.Util;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class Myadapter extends FragmentPagerAdapter{
public static String string[] = { "課程", "問答", "求課", "學(xué)習(xí)", "計劃" };
public Myadapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int arg0) {
MyFragment myfragment = new MyFragment(arg0);
return myfragment;
}
public int getCount() {
return string.length;
}
public CharSequence getPageTitle(int position) {
return string[position];
}
}
top.xml代碼
<?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="wrap_content"
android:background="#07a7a5"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/idx_logo"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="慕課網(wǎng)"
android:gravity="center_vertical"
android:layout_marginLeft="5dp"
android:textColor="#ffffff"
android:textSize="16sp"
android:textStyle="bold"
/>
</LinearLayout>