android中的tab小结

android中的tab,其实很简单,几个步骤

1 首先是一个布局文件,一般是framelayout中有多个linarlayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:id="@+id/widget_layout_Blue"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText android:id="@+id/widget34" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="EditText"
android:textSize="18sp">
</EditText>
<Button android:id="@+id/widget30" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button">
</Button>
</LinearLayout>
<LinearLayout android:id="@+id/widget_layout_red"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical"  >
<AnalogClock android:id="@+id/widget36"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</AnalogClock>
</LinearLayout>
<LinearLayout android:id="@+id/widget_layout_green"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<RadioGroup android:id="@+id/widget43"
android:layout_width="166px" android:layout_height="98px"
android:orientation="vertical">
<RadioButton android:id="@+id/widget44"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="RadioButton">
</RadioButton>
<RadioButton android:id="@+id/widget45"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="RadioButton">
</RadioButton>
</RadioGroup>
</LinearLayout>
</FrameLayout>


2 实际代码:
  public class MyTab extends TabActivity implements OnTabChangeListener {

private TabHost myTabhost;

}
注意这里是继承TabActivity了,之后的TabHost用来容纳每一个标签页,还可以包含任何类型的视图.

protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

myTabhost=this.getTabHost();
myTabhost.addTab(myTabhost.newTabSpec("One").setIndicator("A",getResources().getDrawable(R.drawable.gimp)).setContent(R.id.widget_layout_Blue));
.........

myTabhost.setCurrentTab(0);


  这里,addTab()用来给TabHost上添加一个页;
newTabSpec方法的参数指定该TAB项的ID值得,当在不同的tab切换时,TABHOST会发送不同的ontabchange消息,消息的参数就是该id值.
  setIndicator用于指定标签页上显示的内容,还可以加图标.
  setcontent()用来指定该tab页对应的视图.

对事件的响应:
  public void onTabChanged(String tagString) {
// TODO Auto-generated method stub
if (tagString.equals("One")) {
myMenuSettingTag = 1;
Toast.makeText(getApplicationContext(), "tabid="+tagString,Toast.LENGTH_LONG).show();

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android的PageView Tab通常是通过ViewPager和TabLayout实现的。ViewPager是一个可以滑动切换页面的控件,而TabLayout则是一个可以显示Tab标签的控件。下面是一个简单的实现步骤: 1. 在xml文件添加一个ViewPager和一个TabLayout: ``` <androidx.viewpager.widget.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent"/> <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" app:tabTextColor="@android:color/white" app:tabSelectedTextColor="@android:color/white" app:tabIndicatorColor="@android:color/white"/> ``` 2. 创建一个PagerAdapter来管理ViewPager的页面: ``` public class MyPagerAdapter extends FragmentPagerAdapter { private List<Fragment> fragments; private List<String> titles; public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) { super(fm); this.fragments = fragments; this.titles = titles; } @NonNull @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } @Nullable @Override public CharSequence getPageTitle(int position) { return titles.get(position); } } ``` 3. 在Activity或Fragment设置ViewPager和TabLayout: ``` List<Fragment> fragments = new ArrayList<>(); fragments.add(new Fragment1()); fragments.add(new Fragment2()); fragments.add(new Fragment3()); List<String> titles = new ArrayList<>(); titles.add("Tab1"); titles.add("Tab2"); titles.add("Tab3"); ViewPager viewPager = findViewById(R.id.view_pager); TabLayout tabLayout = findViewById(R.id.tab_layout); viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), fragments, titles)); tabLayout.setupWithViewPager(viewPager); ``` 这样就可以实现一个简单的PageView Tab了。你可以根据自己的需求定制TabLayout的样式和ViewPager的页面内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值