如何添加在机器人动作条的文本的顶部图标 [英] how to add icon on the top of text of actionbar in android

查看:105
本文介绍了如何添加在机器人动作条的文本的顶部图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来android.I想实现它有三个选项卡1动作条,每个标签包含一个图标,tab.I上午的名义成功地将每个选项卡上的图标和文字,但不幸的是图标左边来了在标签文本(标签名称)的一面。我想要的文字,而不是左侧顶部的位置图标。请找我的code的片段,并请帮我找到解决方案。由于提前,

 私人无效setActionBar()         {           动作条酒吧= getActionBar();    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayShowHomeEnabled(假);
    bar.setDisplayShowTitleEnabled(假);    ActionBar.Tab塔巴= bar.newTab()的setText(塔巴);
        tabA.setIcon(R.drawable.iconA);            ActionBar.Tab塔布= bar.newTab()的setText(泰伯);
    tabB.setIcon(R.drawable.iconB);    ActionBar.Tab TABC = bar.newTab()的setText(TABC);
            tabC.setIcon(R.drawable.iconC);
        }


解决方案

您可以使用自定义视图来定义你想怎么都显示你的标签。


  1. 自定义的布局,你有你的文字上面的图片

  2. 在您的活动,膨胀的观点和图像设置的值和文本

  3. 的选项卡中设置自定义视图

下面是一个粗略的例如:

自定义布局

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:重力=CENTER_HORIZONTAL
    机器人:方向=垂直>< ImageView的
    机器人:ID =@ + ID / tabIcon
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =CENTER_HORIZONTAL
    机器人:paddingTop =2DP/><的TextView
    机器人:ID =@ + ID / tabText
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =CENTER_HORIZONTAL
    机器人:文字颜色=#FFFFFF/>< / LinearLayout中>

INFLATE VIEW

 查看TabView的= activity.getLayoutInflater()膨胀(R.layout.actiobar_tab,NULL);
TextView的tabText =(TextView中)tabView.findViewById(R.id.tabText);
tabText.setText(R.String.sometext);ImageView的tabImage =(ImageView的)tabView.findViewById(R.id.tabIcon);
tabImage.setImageDrawable(activity.getResources()getDrawable(R.drawable.someimage));

设置自定义视图对于给定的TAB

 设置页标签= actionBar.newTab()。setCustomView(TabView的)

I am new to android.I am trying to implement one actionbar which has three tabs,each tab contains one icon and name of tab.I am succeeded to place icon and text on each tab,but unfortunately icon is coming on left side of text(name of the tab) in tab. I want place icon on the top of the text instead of left side. Please find snippet of my code, and please help me to find solution. Thanks in advance,

        private void setActionBar()

         {               

           ActionBar bar = getActionBar();

    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayShowHomeEnabled(false);
    bar.setDisplayShowTitleEnabled(false);

    ActionBar.Tab tabA = bar.newTab().setText("TabA");
        tabA.setIcon(R.drawable.iconA);

            ActionBar.Tab tabB = bar.newTab().setText("TabB");
    tabB.setIcon(R.drawable.iconB);

    ActionBar.Tab tabC = bar.newTab().setText("TabC");
            tabC.setIcon(R.drawable.iconC);
        }

解决方案

You can use custom view to define how you want to have your tabs displayed.

  1. define custom layout where you have an image above your text
  2. in your activity, inflate the view and set the values for your image and your text
  3. set custom view for the tab

Here is a rough example:

CUSTOM LAYOUT

<?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:gravity="center_horizontal"
    android:orientation="vertical" >

<ImageView
    android:id="@+id/tabIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingTop="2dp" />

<TextView
    android:id="@+id/tabText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:textColor="#FFFFFF" />

</LinearLayout>

INFLATE VIEW

View tabView = activity.getLayoutInflater().inflate(R.layout.actiobar_tab, null);
TextView tabText = (TextView) tabView.findViewById(R.id.tabText);
tabText.setText(R.String.sometext);

ImageView tabImage = (ImageView) tabView.findViewById(R.id.tabIcon);
tabImage.setImageDrawable(activity.getResources().getDrawable(R.drawable.someimage));

SET CUSTOM VIEW FOR THE GIVEN TAB

Tab tab = actionBar.newTab().setCustomView(tabView)

这篇关于如何添加在机器人动作条的文本的顶部图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆