简体   繁体   English

Android标签栏有不同的片段和不同的操作栏图标

[英]Android tab bar with different fragments and different action bar icons

Problem Description and Question 问题描述和问题

I'm using tab activity with action bar. 我正在使用带有操作栏的标签活动。 In the HOME tab I have GridView which contains some items, like it is shown in the image below (Hot Lines, Taxi, Restaurants etc) 在HOME选项卡中,我有GridView ,其中包含一些项目,如下图所示(热线,出租车,餐馆等)

在此输入图像描述

Wen user click on the items in the grid view I want my application to take following actions: Wen用户点击网格视图中的项目我希望我的应用程序采取以下操作:

  • Change icon of application to grid view item image on which I pressed. 将应用程序图标更改为我按下的网格视图项目图像。
  • Change the test near the icon 更改图标附近的测试
  • Add standard back button near icon, which will go back to grid view screen. 在图标附近添加标准后退按钮,将返回到网格视图屏幕。
  • Change the tab fragment to the one which I specify. 将选项卡片段更改为我指定的选项卡片段。

Like it is shown in the image below: 如下图所示:

在此输入图像描述

As I never deal with this kind of problems can you please give me example or share some link with me of how I can do this? 由于我从不处理这类问题,请你举个例子或与我分享一些如何做到这一点的链接? and can I do this at all? 我可以这样做吗?

This might help: Android studio - is possible to add tabs pointing to fragments from designer? 这可能会有所帮助: Android studio - 是否可以添加指向设计师片段的标签? It is not exactly what you want, but a good start. 这不是你想要的,但是一个好的开始。 If you are willing to put a bit of work in it, you should be able to get what you want. 如果你愿意做一些工作,你应该能够得到你想要的东西。 If you have a basic Frame to work with and more specific problems with this matter I will gladly help you out ^^ 如果你有一个基本的框架与这个问题和更具体的问题,我会很乐意帮助你^^

John 约翰

The first link you can check is THIS . 您可以检查的第一个链接是这个

And you should read more about ActionBar . 你应该阅读更多关于ActionBar的内容

The last thing is it's better if you google it first and try to write a code and when you got stuck somewhere share your code with us and ask for help. 最后一件事是,如果你首先谷歌它并尝试编写代码,当你遇到某个地方并与我们分享你的代码并寻求帮助时,它会更好。

You have to use actionbarsherlock library for it. 你必须使用actionbarsherlock库。

use android.support.v4.app.FragmentTabHost and TabWidget in the xml as shown below : 在xml中使用android.support.v4.app.FragmentTabHostTabWidget ,如下所示:

<android.support.v4.app.FragmentTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    style="@style/yourstyle"/>

    <FrameLayout
        android:id="@+id/realtabcontent"
        style="@style/realtabFrameContentStyle" />


       <TabWidget
          android:id="@android:id/tabs"
         style="@style/yourtabstyle" />

</android.support.v4.app.FragmentTabHost>

Use SherlockFragmentActivity for showing the tabs. 使用SherlockFragmentActivity显示选项卡。

In the activities code use following code (preferably in a function) to set the ctionbar icon and text : 在活动代码中使用以下代码(最好在函数中)来设置ctionbar图标和文本:

activity.getSupportActionBar().setDisplayShowCustomEnabled(true);
activity.getSupportActionBar().setCustomView(R.layout.your_action_barlayout);


((TextView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.action_bar_title))).setText("your title");

        ImageView homeButton = ((ImageView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.your_icon)));
        homeButton.setVisibility(View.VISIBLE);
        homeButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, YourHOmeActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                activity.startActivity(intent);
                activity.finish();
            }
        });
        ActionBar mActionBar = activity.getSupportActionBar();
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        mActionBar.show();

then call this function with your icon and your text in your fragments onResume() method. 然后在片段onResume()方法中使用您的图标和文本调用此函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM