布局
<<FONT face=宋体>android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="50dp"
app:tabTextColor="#f00"
app:tabSelectedTextColor="#0f0"
app:tabIndicatorColor="#00f"
/>
<<FONT face=宋体>android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
代码
package com.bwie.cehuatablayout;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import java.util.ArrayList;
import java.util.List;
import static android.R.id.list;
public class MainActivity extends AppCompatActivity {
private SlidingMenu slidingMenu;
private TextView tv1;
private TabLayout tab;
private ViewPager vp;
private String[] titlearr;
private ArrayList list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
titlearr = new String[]{"标题1","标题2","标题3","标题4","标题5","标题6","标题7","标题8","标题11","标题1","标题1"};
setContentView(R.layout.activity_main);
TextView tv= (TextView) findViewById(R.id.tv);
//TabLayout侧滑
tab = (TabLayout) findViewById(R.id.tab);
vp = (ViewPager) findViewById(R.id.vp);
slidingMenu = new SlidingMenu(this);
slidingMenu.setMode(SlidingMenu.LEFT);
slidingMenu.setBehindOffset(100);
slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);
slidingMenu.setMenu(R.layout.left_menu);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
slidingMenu.toggle();
}
});
//侧滑的属性
tv1 = (TextView) findViewById(R.id.tv1);
tv1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv1.setText("我是更改后的内容");
}
});
initVP();
tab.setTabMode(TabLayout.MODE_SCROLLABLE);
tab.setupWithViewPager(vp);
}
private void initVP() {
list = new ArrayList();
for (int i = 0; i <<SPAN style="COLOR: #0000ff">11; i++) {
TextView tv = new TextView(this);
tv.setText("标题" + i);
tv.setTextColor(Color.BLACK);
tv.setTextSize(30);
list.add(tv);
}
MyPagerAdapter adapter = new MyPagerAdapter();
vp.setAdapter(adapter);
}
class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(list.get(position));
return list.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public CharSequence getPageTitle(int position) {
return titlearr[position];
}
}
}