在 ActionBarActivity 中设置底部的 ActionBar Tab

2023-12-02

你好,我是 Android 菜鸟。

我在用appcompat支持库添加选项卡ActionBarActivity。我已经编写了要添加的代码,但选项卡显示在顶部,请在屏幕截图中看到它。我想设置这些Tabs这将出现在底部。

知道为什么会这样吗?

private ActionBar mActionBar;

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

    // find the action bar
    mActionBar = getSupportActionBar();

    // First Tab of the Activity
    ActionBar.Tab mTab = mActionBar.newTab().setText("First Tab").setTabListener(this);
    mActionBar.addTab(mTab);
    mActionBar.selectTab(mTab);

    // Second Tab of the Activity
    mTab = mActionBar.newTab().setText("Second Tab").setTabListener(this);
    mActionBar.addTab(mTab);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTrasaction) {
    if(tab.getPosition() == 0) {
        fragmentTrasaction.replace(R.id.container, new FirstFragment());
    } else {
        fragmentTrasaction.replace(R.id.container, new SecondFragment());
    }
}

@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}

截屏

enter image description here


您可以在屏幕的任何位置创建自己的 TabView Bar,例如在底部 非常适合交换片段 在tags_icon.xml中简单的自定义视图

这是底部TabView

主要活动类别

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

public class TagsActivity extends Activity {
    public static final String M_CURRENT_TAB = "M_CURRENT_TAB";
    private TabHost mTabHost;
    private String mCurrentTab;

    public static final String TAB_TAGS = "TAB_TAGS";
    public static final String TAB_MAP = "TAB_MAP";
    public static final String TAB_SETTINGS = "TAB_SETTINGS";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        getActionBar().hide();
        setContentView(R.layout.tags_activity);

        mTabHost = (TabHost) findViewById(android.R.id.tabhost);

        mTabHost.setup();

        if (savedInstanceState != null) {
            mCurrentTab = savedInstanceState.getString(M_CURRENT_TAB);
            initializeTabs();
            mTabHost.setCurrentTabByTag(mCurrentTab);
            /*
            when resume state it's important to set listener after initializeTabs
            */
            mTabHost.setOnTabChangedListener(listener);
        } else {
            mTabHost.setOnTabChangedListener(listener);
            initializeTabs();
        }
    }

    private View createTabView(final int id, final String text) {
        View view = LayoutInflater.from(this).inflate(R.layout.tabs_icon, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.tab_icon);
        imageView.setImageDrawable(getResources().getDrawable(id));
        TextView textView = (TextView) view.findViewById(R.id.tab_text);
        textView.setText(text);
        return view;
    }

    /*
    create 3 tabs with name and image
    and add it to TabHost
     */
    public void initializeTabs() {

        TabHost.TabSpec spec;

        spec = mTabHost.newTabSpec(TAB_TAGS);
        spec.setContent(new TabHost.TabContentFactory() {
            public View createTabContent(String tag) {
                return findViewById(R.id.realtabcontent);
            }
        });
        spec.setIndicator(createTabView(R.drawable.tab_tag_drawable, getString(R.string.tab_tags)));
        mTabHost.addTab(spec);

        spec = mTabHost.newTabSpec(TAB_MAP);
        spec.setContent(new TabHost.TabContentFactory() {
            public View createTabContent(String tag) {
                return findViewById(R.id.realtabcontent);
            }
        });
        spec.setIndicator(createTabView(R.drawable.tab_map_drawable, getString(R.string.tab_map)));
        mTabHost.addTab(spec);


        spec = mTabHost.newTabSpec(TAB_SETTINGS);
        spec.setContent(new TabHost.TabContentFactory() {
            public View createTabContent(String tag) {
                return findViewById(R.id.realtabcontent);
            }
        });
        spec.setIndicator(createTabView(R.drawable.tab_settings_drawable, getString(R.string.tab_settings)));
        mTabHost.addTab(spec);

    }

    /*
    first time listener will be trigered immediatelly after first: mTabHost.addTab(spec);
    for set correct Tab in setmTabHost.setCurrentTabByTag ignore first call of listener
    */
    TabHost.OnTabChangeListener listener = new TabHost.OnTabChangeListener() {
        public void onTabChanged(String tabId) {

            mCurrentTab = tabId;

            if (tabId.equals(TAB_TAGS)) {
                pushFragments(SearchFragment.getInstance(), false,
                        false, null);
            } else if (tabId.equals(TAB_MAP)) {
                pushFragments(MapContainerFragment.getInstance(), false,
                        false, null);
            } else if (tabId.equals(TAB_SETTINGS)) {
                pushFragments(SettingsFragment.getInstance(), false,
                        false, null);
            }

        }
    };

/*
Example of starting nested fragment from another fragment:

Fragment newFragment = ManagerTagFragment.newInstance(tag.getMac());
                TagsActivity tAct = (TagsActivity)getActivity();
                tAct.pushFragments(newFragment, true, true, null);
 */
    public void pushFragments(Fragment fragment,
                              boolean shouldAnimate, boolean shouldAdd, String tag) {
        FragmentManager manager = getFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        if (shouldAnimate) {
            ft.setCustomAnimations(R.animator.fragment_slide_left_enter,
                    R.animator.fragment_slide_left_exit,
                    R.animator.fragment_slide_right_enter,
                    R.animator.fragment_slide_right_exit);
        }
        ft.replace(R.id.realtabcontent, fragment, tag);

        if (shouldAdd) {
            /*
            here you can create named backstack for realize another logic.
            ft.addToBackStack("name of your backstack");
             */
            ft.addToBackStack(null);
        } else {
            /*
            and remove named backstack:
            manager.popBackStack("name of your backstack", FragmentManager.POP_BACK_STACK_INCLUSIVE);
            or remove whole:
            manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
             */
            manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
        ft.commit();
    }

    /*
    If you want to start this activity from another
     */
    public static void startUrself(Activity context) {
        Intent newActivity = new Intent(context, TagsActivity.class);
        newActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(newActivity);
        context.finish();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putString(M_CURRENT_TAB, mCurrentTab);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onBackPressed(){
        super.onBackPressed();
    }
}

标签_活动.xml

<?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="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0"/>
            <FrameLayout
                android:id="@android:id/realtabcontent"
                android:background="@drawable/bg_main_app_gradient"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
            <TabWidget
                android:id="@android:id/tabs"
                android:background="#EAE7E1"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"/>
        </LinearLayout>
    </TabHost>

标签图标.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tabsLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg_tab_gradient"
    android:gravity="center"
    android:orientation="vertical"
    tools:ignore="contentDescription" >

    <ImageView
        android:id="@+id/tab_icon"
        android:layout_marginTop="4dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView 
        android:id="@+id/tab_text"
        android:layout_marginBottom="3dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/tab_text_color"/>

</LinearLayout>

enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 ActionBarActivity 中设置底部的 ActionBar Tab 的相关文章

随机推荐

  • Prolog - 复制一段列表

    我需要在序言中复制列表 我有清单 L a string1 value1 a string2 value2 a string3 value3 a string4 value4 输出将是 L string1 string2 string3 st
  • 同时在两个 PHP 脚本中使用相同的会话 ID

    我偶尔会发现 PHP 会话存在一个奇怪的问题 当我使用相同的会话 ID 运行两个 PHP 脚本时 第二个脚本会卡住 直到第一个脚本完成 我猜这是因为尝试打开同一会话存储文件两次 但可能我不对 在正常的网站工作中您永远不会发现这种效果 因为用
  • Asp.NET targetFramework 版本和 IIS 应用程序池版本不匹配

    最近我一直在 IIS 中为 Asp Net 网站创建应用程序池 让我感到惊讶的是 这些应用程序池具有 v4 0 而网站则针对 Net Framework v4 5 v4 5 1 各个网站的 Web config 中的条目 这就是应用程序池设
  • 如何将 OpenCV 集成到 Qt Creator Android 项目中

    我使用 Qt Creator 编译 Android 应用程序 我需要将 OpenCV 集成到其中 我花了半天时间才正确配置它 所以我想记录一下我在这里采取的步骤 以防其他人必须这样做 Edit 对于 OpenCV 4 x 请参阅下面的答案
  • 如何在 Nginx 代理服务器中启用 CORS?

    正如我的标题 这是位于conf d api server conf 中的配置文件 server listen 80 server name api localhost location add header Access Control A
  • TextArea MaxLength - 支持还是不支持?

    我正准备添加一个 jQuery 插件来支持文本区域的 maxlength 并注意到 MaxLength 属性在 Safari Chrome 和 Firefox 上本机工作 这到底是HTML5的功劳还是 这是否意味着文本区域的 maxleng
  • 具有多索引的 Pandas 数据透视表小计

    我正在尝试创建一个带有小计 Excel 风格的简单数据透视表 但是我找不到使用 Pandas 的方法 我已经尝试了韦斯在另一个与小计相关的问题中建议的解决方案 但这并没有给出预期的结果 下面是重现它的步骤 创建样本数据 sample dat
  • pdf 小丑 - 不突出显示特定搜索关键字

    我正在使用 pdf clown 和 pdfclown 0 2 0 HEAD jar 我编写了下面的代码来突出显示中文 pdf 文件中的关键字搜索 相同的代码在英文 pdf 文件中工作正常 import java awt Color impo
  • 使用 jQuery 的轮播

    我知道那里有可用的插件 但我正在尝试制作一个自己的插件 但在此之前我试图理解将其制作为无限 圆形轮播的概念 这是我的到目前为止 http jsfiddle net hbk35 KPKyz 3 HTML div ul li div 0 div
  • Div "contenteditable" :获取和删除插入符号之前的单词

    谢谢这个问题和 Tim Down 发布的答案 我做了一个函数来获取 contenteditable div 中插入符号之前的单词 这是一个fiddle 这是函数 function getWordPrecedingCaret containe
  • 从 powershell 为 azure 函数创建函数键

    是否有可能为刚刚从 powershell 脚本创建的 azure 函数创建一个函数键 我有一个发布管道来为 azure 函数创建整个环境 它工作正常 但我缺少的一部分是该函数的自定义函数键 我不想使用默认密钥 我可以在门户中创建新密钥 但我
  • 使用 lambda 函数对 filter() 进行复杂性分析

    给定两个列表 list1 and list2 list3 filter lambda x x in list1 list2 这将返回两个列表的交集 我怎样才能找到这个算法的复杂度 我发现时间复杂度x in list1 is O n 其中 n
  • 理解/mySQL 又名欺骗 Django 中的外键关系

    所以我继承了一些django mySQL 表非常简单 其中父级不是 FK 关系 只是 父级 id CREATE TABLE Child id int 10 unsigned NOT NULL AUTO INCREMENT parent in
  • ASP.NET 中带有和不带有委托的事件

    在一些 ASP NET 示例中 我看到事件与委托一起使用像这样有时没有他们像这样 请解释 所有事件都是委托类型 它们都继承自EventHandler继承自MulticastDelegate其间断自Delegate 有时 或者我宁愿说大多数时
  • 检查 URL 是否转到包含文本“404”的页面

    我有一个 bash 脚本来检查 URL 列表的 HTTP 状态代码 但我意识到有些虽然看起来是 200 但实际上显示包含 错误 404 的页面 我该如何检查呢 这是我当前的脚本 bin bash while read LINE do cur
  • Javascript-firefox:如何从本地 png 文件设置自定义光标?

    如何从本地图像文件为当前页面设置自定义光标 基本上我需要使用 javascript 以编程方式更改为不同的光标 但光标本地存储在我的硬盘驱动器上 你不能 但见下文 因为file 不能从其他协议访问或引用 通过 JavaScript 包含光标
  • 使用 预加载 JSON 文件

    我正在使用 Angular 5 应用程序 并且正在加载相当大的 JSON 文件 问题是加载main需要很长时间 js捆绑 初始化它 引导 Angular 应用程序 稍后获取该 JSON 文件 如果我可以在 Angular 应用程序准备就绪时
  • 如何通过php脚本发送邮件?

    我如何通过php脚本发送邮件 我正在尝试做这样的事情 for k 0 k lt x gt length 1 k for l 0 l lt j 1 l if y gt item k gt nodeValue JobNoArr l Accept
  • c++中的std::string有编码格式吗

    我想找到关于std string的默认编码格式 我想找出编码格式 但我不知道 c 中的std string有编码格式吗 简单的答案 std string定义为std basic string
  • 在 ActionBarActivity 中设置底部的 ActionBar Tab

    你好 我是 Android 菜鸟 我在用appcompat支持库添加选项卡ActionBarActivity 我已经编写了要添加的代码 但选项卡显示在顶部 请在屏幕截图中看到它 我想设置这些Tabs这将出现在底部 知道为什么会这样吗 pri