Android编程实现在底端显示选项卡的方法

本文实例讲述了Android编程实现在底端显示选项卡的方法。分享给大家供大家参考,具体如下:

1.layout 文件

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
  <TabHost android:id="@+id/edit_item_tab_host"
  android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">
      <FrameLayout android:id="@android:id/tabcontent"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:padding="5dp" android:layout_weight="1">
        <LinearLayout android:id="@+id/widget_layout_Blue"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
          <RelativeLayout android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:paddingLeft="3px"
          android:paddingRight="3px">
            <LinearLayout android:id="@+id/titleLayout_person_check_road_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            </LinearLayout>
            <LinearLayout
            android:id="@+id/layout_person_check_road_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/titleLayout_person_check_road_add"
            android:layout_alignTop="@id/titleLayout_person_check_road_add"
            android:orientation="vertical">
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
    <LinearLayout android:id="@+id/widget_layout_red"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
      <RelativeLayout android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:paddingLeft="3px" android:paddingRight="3px">
        <LinearLayout android:id="@+id/titleLayout_person_check_road_add1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        </LinearLayout>
        <LinearLayout
        android:id="@+id/layout_person_check_road_add1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/titleLayout_person_check_road_add1"
        android:layout_alignTop="@id/titleLayout_person_check_road_add1"
        android:orientation="vertical">
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>
<LinearLayout android:id="@+id/widget_layout_green"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
  <RelativeLayout android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:paddingLeft="3px" android:paddingRight="3px">
    <LinearLayout android:id="@+id/titleLayout_person_check_road_add2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"> </LinearLayout>
    <LinearLayout android:id="@+id/layout_person_check_road_add2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/titleLayout_person_check_road_add2"
    android:layout_alignTop="@id/titleLayout_person_check_road_add2"
    android:orientation="vertical"> </LinearLayout>
  </RelativeLayout>
</LinearLayout>
<LinearLayout android:id="@+id/widget_layout_yellow"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
  <RelativeLayout android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:paddingLeft="3px" android:paddingRight="3px">
    <LinearLayout android:id="@+id/titleLayout_person_check_road_add3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"> </LinearLayout>
    <LinearLayout android:id="@+id/layout_person_check_road_add3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/titleLayout_person_check_road_add3"
    android:layout_alignTop="@id/titleLayout_person_check_road_add3"
    android:orientation="vertical"> </LinearLayout>
  </RelativeLayout>
</LinearLayout>
</FrameLayout>
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="0" /> </LinearLayout>
</TabHost>
</LinearLayout>

2.Java 文件

import android.app.ActivityGroup;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TabHost;
import android.widget.TextView;
public class TabBottom extends ActivityGroup {
  public static TabHost myTabhost;
  private LayoutParams title_params = new LayoutParams(120, 50);
  private LayoutParams content_params = new LayoutParams(158, 50);
  private LinearLayout titleLayout, showViewLayout, titleLayout1,
  showViewLayout1, titleLayout2, showViewLayout2, titleLayout3,
  showViewLayout3;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bottomtab);
    myTabhost = (TabHost) findViewById(R.id.edit_item_tab_host);
    myTabhost.setup(this.getLocalActivityManager());
    titleLayout = (LinearLayout) findViewById(R.id.titleLayout_person_check_road_add);
    showViewLayout = (LinearLayout) findViewById(R.id.layout_person_check_road_add);
    titleLayout1 = (LinearLayout) findViewById(R.id.titleLayout_person_check_road_add1);
    showViewLayout1 = (LinearLayout) findViewById(R.id.layout_person_check_road_add1);
    titleLayout2 = (LinearLayout) findViewById(R.id.titleLayout_person_check_road_add2);
    showViewLayout2 = (LinearLayout) findViewById(R.id.layout_person_check_road_add2);
    titleLayout3 = (LinearLayout) findViewById(R.id.titleLayout_person_check_road_add3);
    showViewLayout3 = (LinearLayout) findViewById(R.id.layout_person_check_road_add3);
    myTabhost.addTab(myTabhost
    .newTabSpec("One")
    .setIndicator("收件箱",
    getResources().getDrawable(R.drawable.icon))
    .setContent(R.id.widget_layout_Blue));
    myTabhost.addTab(myTabhost
    .newTabSpec("Two")
    .setIndicator("发件箱",
    getResources().getDrawable(R.drawable.icon))
    .setContent(R.id.widget_layout_green));
    myTabhost.addTab(myTabhost
    .newTabSpec("Three")
    .setIndicator("垃圾箱",
    getResources().getDrawable(R.drawable.icon))
    .setContent(R.id.widget_layout_red));
    myTabhost.addTab(myTabhost
    .newTabSpec("Four")
    .setIndicator("发送",
    getResources().getDrawable(R.drawable.icon))
    .setContent(R.id.widget_layout_yellow));
    showInLayout();
    showInLayout1();
    showInLayout2();
    showInLayout3();
  }
  /*
  * 填充第一个选项卡页面
  */
  private void showInLayout() {
    for (int i = 0; i < 10; i++) {
      String condName = "名称显示1";
      TextView tv = new TextView(this);
      tv.setTextColor(Color.WHITE);
      tv.setTextSize(16);
      tv.setLayoutParams(title_params);
      tv.setText(condName + ":");
      titleLayout.addView(tv);
      EditText tv1 = new EditText(this);
      tv1.setTextSize(16);
      tv1.setLayoutParams(content_params);
      tv1.setText(condName);
      showViewLayout.addView(tv1);
    }
  }
  private void showInLayout1() {
    for (int i = 0; i < 10; i++) {
      String condName = "名称显示2";
      TextView tv = new TextView(this);
      tv.setTextColor(Color.WHITE);
      tv.setTextSize(16);
      tv.setLayoutParams(title_params);
      tv.setText(condName + ":");
      titleLayout1.addView(tv);
      TextView tv1 = new TextView(this);
      tv1.setTextColor(Color.WHITE);
      tv1.setTextSize(16);
      tv1.setLayoutParams(content_params);
      tv1.setText(condName + ":");
      showViewLayout1.addView(tv1);
    }
  }
  private void showInLayout2() {
    for (int i = 0; i < 10; i++) {
      String condName = "名称显示3";
      TextView tv = new TextView(this);
      tv.setTextColor(Color.WHITE);
      tv.setTextSize(16);
      tv.setLayoutParams(title_params);
      tv.setText(condName + ":");
      titleLayout2.addView(tv);
      TextView tv1 = new TextView(this);
      tv1.setTextColor(Color.WHITE);
      tv1.setTextSize(16);
      tv1.setLayoutParams(content_params);
      tv1.setText(condName + ":");
      showViewLayout2.addView(tv1);
    }
  }
  private void showInLayout3() {
    for (int i = 0; i < 10; i++) {
      String condName = "名称显示4";
      TextView tv = new TextView(this);
      tv.setTextColor(Color.WHITE);
      tv.setTextSize(16);
      tv.setLayoutParams(title_params);
      tv.setText(condName + ":");
      titleLayout3.addView(tv);
      TextView tv1 = new TextView(this);
      tv1.setTextColor(Color.WHITE);
      tv1.setTextSize(16);
      tv1.setLayoutParams(content_params);
      tv1.setText(condName + ":");
      showViewLayout3.addView(tv1);
    }
  }
}

效果如下:

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android编程之activity操作技巧总结》、《Android资源操作技巧汇总》、《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android编程开发之SD卡操作方法汇总》、《Android视图View技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

(0)

相关推荐

  • android TabHost(选项卡)的使用方法

    首先,定义TabHost的布局文件: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/tabhost" android:layout_width="fill_p

  • Android多个TAB选项卡切换效果

    在前一期中,我们做了悬浮头部的两个tab切换和下拉刷新效果,后来项目中要求改成三个tab,当时就能估量了一下,如果从之前的改,也不是不可以,但是要互相记住的状态就太多了,很容易出现错误.就决定重新实现一下这个效果,为此先写了一个demo,这期间项目都已经又更新了两个版本了.demo还木有变成文章. 之前的版本中是采用了一个可以下拉刷新的listview,之后在listview中添加了两个头部,并且在该布局上的上面用了一个一模一样的切换tab,如果没有看过前面版本的,可以看看前一个版本,Listv

  • android 选项卡(TabHost)如何放置在屏幕的底部

    今天写Tab的时候由于TAB的跳转问题去查资料,倒反而发现更有趣的问题,就是如何将TAB放置在屏幕的底端. 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" a

  • Android基于ViewPager Fragment实现选项卡

    先给大家展示效果图: 1.新建TestFragmen继承Fragment public class TestFragment extends Fragment { private static final String TAG = "TestFragment"; private String hello;// = "hello android"; private String defaultHello = "default value"; pri

  • Android实现底部导航栏功能(选项卡)

    现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其中各个类的作用以及资源文件就不详细解释了,还有资源图片(在该Demo中借用了其它应用程序的资源图片)也不提供了,大家可以自行更换自己需要的资源图片.直接上各个布局文件或各个类的代码: 1. res/layout目录下的 maintabs.xml 源码: <?xml version="1.0&q

  • Android编程之TabWidget选项卡用法实例分析

    本文实例讲述了Android编程之TabWidget选项卡用法.分享给大家供大家参考,具体如下: 1 概览 TabWidget与TabHost.tab组件一般包括TabHost和TabWidget.FrameLayout,且TabWidget.FrameLayout属于TabHost. 是否继承TabActivity的问题 实现步骤.两种实现方式,一种是将每个Tab的布局嵌在TabHost中的FrameLayout中,每个Tab的内容布局与显示都在FrameLayout中进行,缺点是布局会显得很

  • Android仿微信底部实现Tab选项卡切换效果

    在网上看了比较多的关于Tab的教程,发现都很杂乱.比较多的用法是用TitlePagerTabStrip和ViewPaper.不过TitlePagerTabStrip有个很大的缺陷,Tab里面的内容刚进去是没有的,要滑一次才能加载出来.而且滑动的时候,Tab里面的内容位置不是固定的,滑倒最后会出现一片空白,非常不美观.虽然有其他的补救方法,但是非常的麻烦,所以我就按照自己的方法实现了一个,功能不错而且非常简单. 直接点击或者是滑动界面,都可以转到相应的页面. 效果图: 原理是用了三个按钮和View

  • Android利用Fragment实现Tab选项卡效果

    利用Fragment实现Tab选项卡效果:  将RadioGroup与Fragment集合,实现tab选项卡效果,这里面最关键的几个文件: 1.FragmentTabAdapter类: /** *@Description: *@Author:Nate Robinson *@Since:2015-2-12 */ public class FragmentTabAdapter implements RadioGroup.OnCheckedChangeListener { private List<F

  • Android编程实现自定义Tab选项卡功能示例

    本文实例讲述了Android编程实现自定义Tab选项卡功能.分享给大家供大家参考,具体如下: import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.widget.*; import android.widget.TabHost.OnTabChangeListener; import android.os.Build; import androi

  • Android组件TabHost实现页面中多个选项卡切换效果

    TabHost组件可以在界面中存放多个选项卡, 很多软件都使用了改组件进行设计. 一.基础知识 TabWidget : 该组件就是TabHost标签页中上部 或者 下部的按钮, 可以点击按钮切换选项卡; TabSpec : 代表了选项卡界面, 添加一个TabSpec即可添加到TabHost中; -- 创建选项卡 : newTabSpec(String tag), 创建一个选项卡; -- 添加选项卡 : addTab(tabSpec); 二.实例讲解 TabHost的基本使用,主要是layout的

  • Android TabLayout(选项卡布局)简单用法实例分析

    本文实例讲述了Android TabLayout(选项卡布局)简单用法.分享给大家供大家参考,具体如下: 我们在应用viewpager的时候,经常会使用TabPageIndicator来与其配合.达到很漂亮的效果.但是TabPageIndicator是第三方的,而且比较老了,当然了现在很多大神都已经开始自己写TabPageIndicator来满足自己的需求,在2015年的google大会上,google发布了新的Android Support Design库,里面包含了几个新的控件,其中就有一个

  • Android实现类似网易新闻选项卡动态滑动效果

    本文会实现一个类似网易新闻(不说网易新闻大家可能不知道大概是什么样子)点击超多选项卡,选项卡动态滑动的效果. 首先来看看布局,就是用HorizontalScrollView控件来实现滑动的效果,里面包含了一个布局. 接下来我们在onCreat方法中加载布局和构建我们需要显示的数据 <code class="hljs avrasm"> @Override protected void onCreate(Bundle savedInstanceState) { super.on

随机推荐