Tab页面1

1.效果如下:

1

2.实现思路:

1.目录结构:
23
2.代码编写:
activity_main.xml:

<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">

    <include layout="@layout/top"/>

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/id_view_pager"        
        >
    </android.support.v4.view.ViewPager>
    <include layout="@layout/bottom"/>
</LinearLayout>

bottom.xml:
只给出前两个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:orientation="horizontal"
    android:background="#000000"
     >

    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tab_1"
        android:layout_weight="1"
        android:orientation="vertical"
        >
        <ImageButton 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/img_1"
            android:background="@drawable/ic_launcher"
            />
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab1"
            android:layout_gravity="center"
            />
    </LinearLayout>
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tab_2"
        android:layout_weight="1"
        android:orientation="vertical"
        >
        <ImageButton 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/img_2"
            android:background="@drawable/ic_launcher"
            />
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab2"
            android:layout_gravity="center"
            />
    </LinearLayout>

view1.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="match_parent"
    android:orientation="vertical" >

    <TextView 
        android:id="@+id/view_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="View1"
        android:gravity="center"
        android:textSize="30sp"
        />
</LinearLayout>

MainActivity.java:

    private ViewPager viewPager;
    private PagerAdapter adapter;
    private List<View> viewList = new ArrayList<View>();
    //img
    private ImageButton img1,img2,img3,img4;
    //tab view 
    private LinearLayout tab1,tab2,tab3,tab4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
    }

    private void initEvent() {
        tab1.setOnClickListener(this);
        tab2.setOnClickListener(this);
        tab3.setOnClickListener(this);
        tab4.setOnClickListener(this);

        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                int currentItem = viewPager.getCurrentItem();
                switch(currentItem){
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    private void initView() {
        viewPager = (ViewPager)findViewById(R.id.id_view_pager);
        //tab
        tab1 = (LinearLayout)findViewById(R.id.tab_1);
        tab2 = (LinearLayout)findViewById(R.id.tab_2);
        tab3 = (LinearLayout)findViewById(R.id.tab_3);
        tab4 = (LinearLayout)findViewById(R.id.tab_4);
        //imageButton
        img1 = (ImageButton)findViewById(R.id.img_1);
        img2 = (ImageButton)findViewById(R.id.img_2);
        img3 = (ImageButton)findViewById(R.id.img_3);
        img4 = (ImageButton)findViewById(R.id.img_4);
        //inflate
        LayoutInflater inflater = LayoutInflater.from(this);
        View view1 = inflater.inflate(R.layout.view1, null);
        View view2 = inflater.inflate(R.layout.view2, null);
        View view3 = inflater.inflate(R.layout.view3, null);
        View view4 = inflater.inflate(R.layout.view4, null);

        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        viewList.add(view4);

        adapter = new PagerAdapter() {

            @Override
            public void destroyItem(ViewGroup container, int position,
                    Object object) {
                container.removeView(viewList.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = viewList.get(position);
                container.addView(view);
                return view;
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == arg1;
            }

            @Override
            public int getCount() {
                return viewList.size();
            }
        };
        viewPager.setAdapter(adapter);
    }

    /**
     * 底部的布局点击事件
     */
    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.tab_1:
            viewPager.setCurrentItem(0);
            break;
        case R.id.tab_2:
            viewPager.setCurrentItem(1);
            break;
        case R.id.tab_3:
            viewPager.setCurrentItem(2);
            break;
        case R.id.tab_4:
            viewPager.setCurrentItem(3);
            break;
            default:break;
        }
    }

3.完整代码:

Tab页面实现代码1

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTML中可以通过使用标签来创建选项卡(tab页面。具体步骤如下: 1. 首先,在HTML页面中创建一个具有多个选项卡的容器元素,例如一个div元素。 2. 在容器元素中,创建一个具有选项卡标题的菜单列表。每个选项卡都对应于菜单中的一个项目。 3. 在容器元素中,为每一个选项卡创建一个包含内容的div元素。这些div元素应该具有不同的ID,以便于通过菜单项来切换它们。 4. 使用CSS样式表来隐藏除了第一个选项卡之外的所有内容div元素。可以使用display:none;属性来完成这个任务。 5. 使用JavaScript来添加一个事件监听器,以便在用户点击菜单项时,显示相应的选项卡内容。可以使用classList属性来添加或删除CSS类,以切换可见性。 下面是一个简单的HTML选项卡页面的示例代码(仅供参考): ```html <div class="tab-container"> <ul class="tab-menu"> <li><a href="#tab1">Tab 1</a></li> <li><a href="#tab2">Tab 2</a></li> <li><a href="#tab3">Tab 3</a></li> </ul> <div class="tab-content" id="tab1"> <p>This is the content of tab 1.</p> </div> <div class="tab-content" id="tab2"> <p>This is the content of tab 2.</p> </div> <div class="tab-content" id="tab3"> <p>This is the content of tab 3.</p> </div> </div> <style> .tab-container { border: 1px solid #ccc; } .tab-menu { list-style: none; margin: 0; padding: 0; } .tab-menu li { display: inline-block; margin-right: 10px; } .tab-menu li a { display: block; padding: 5px 10px; background-color: #eee; color: #333; text-decoration: none; } .tab-menu li a.active { background-color: #fff; } .tab-content { display: none; padding: 10px; } .tab-content.active { display: block; } </style> <script> var menuItems = document.querySelectorAll('.tab-menu li a'); var contentItems = document.querySelectorAll('.tab-content'); for (var i = 0; i < menuItems.length; i++) { menuItems[i].addEventListener('click', function(event) { event.preventDefault(); // Remove active class from all menu items for (var j = 0; j < menuItems.length; j++) { menuItems[j].classList.remove('active'); } // Add active class to clicked menu item this.classList.add('active'); // Hide all content items for (var k = 0; k < contentItems.length; k++) { contentItems[k].classList.remove('active'); } // Show content item corresponding to clicked menu item var targetId = this.getAttribute('href'); var targetContent = document.querySelector(targetId); targetContent.classList.add('active'); }); } // Show first tab by default menuItems[0].click(); </script> ``` 这段代码将创建一个具有3个选项卡的容器,每个选项卡都包含一些示例内容。CSS样式表将隐藏所有选项卡内容,JavaScript代码将添加事件监听器来控制选项卡的切换。您可以根据需要修改此代码,并根据自己的需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值