如何在标签的中心展示形象? [英] how to show image at center of tab?

查看:109
本文介绍了如何在标签的中心展示形象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创造了机器人标签仅显示图像(不包含任何文字),我想在标签。还有的中心要显示的图像是多个标签,每个包含图像,但问题是,图像不是在每个标签的中央显示

这是我的XML


    

 < RelativeLayout的
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT>        <的FrameLayout
            机器人:ID =@机器人:ID / tabcontent
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:layout_above =@机器人:ID /标签
             />        < TabWidget
            机器人:ID =@机器人:ID /标签
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentBottom =真
            机器人:背景=#000000>
        < / TabWidget>
    < / RelativeLayout的>< / TabHost>

在code设置图像:

  newsTab.setIndicator(,getResources()。getDrawable(R.drawable.news))setContent。(
                新意图(RestauActivity.this,NewsActivity.class));


解决方案

1) - 创建名为report_tabs.xml或者你喜欢的任何名称的布局文件

在report_tabs使用code。

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
     机器人:ID =@ + ID / layout_tabsLayout
     机器人:layout_width =WRAP_CONTENT
     机器人:layout_height =WRAP_CONTENT
     机器人:背景=@绘制/ btn_selector
     机器人:比重=中心
     机器人:方向=垂直
     >     < ImageView的
     机器人:ID =@ + ID / img_icon
     机器人:layout_width =WRAP_CONTENT
     机器人:layout_height =WRAP_CONTENT
     机器人:layout_gravity =中心
     机器人:layout_marginTop =3DP
     />
    < / LinearLayout中>

2) - 而在你的活动使用下面code

 意图意图;
    tabhost = getTabHost();
    TabHost.TabSpec则tabspec;
    意图=新意图()setClass(getApplicationContext(),xxxxx.class)。    则tabspec = tabhost.newTabSpec(第一);
    鉴于= LayoutInflater.from(本).inflate(R.layout.report_tabs,
tabhost.getTabWidget(),FALSE);    imgtabF =(ImageView的)view.findViewById(R.id.img_icon);
    imgtabF.setBackgroundResource(R.drawable.tab_icon_selector);    tabspec.setIndicator(视图);
    tabspec.setContent(意向);
    tabhost.addTab(则tabspec);

3) - 创建绘制一个名为tab_icon_selector文件更改选项卡上单击该图标是这样的: -

 <?XML版本=1.0编码=UTF-8&GT?;
    <选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <! - 非重点国家 - >
    <项目的android:state_focused =false的机器人:state_selected =假
          机器人:STATE_ pressed =false的机器人:可绘制=@绘制/ medical_icon_unselect
     />
     <项目的android:state_focused =false的机器人:state_selected =真
     机器人:STATE_ pressed =false的机器人:可绘制=@绘制/ medical_icon_sel/>
     <! - 聚焦美国 - >
     <项目的android:state_focused =真正的机器人:state_selected =假
     机器人:STATE_ pressed =false的机器人:可绘制=@绘制/ medical_icon_sel/>
     <项目的android:state_focused =真正的机器人:state_selected =真
     机器人:STATE_ pressed =false的机器人:可绘制=@绘制/ medical_icon_sel/>
     <! - pressed - >
     <项目的android:STATE_ pressed =真正的机器人:可绘制=@机器人:彩色/透明/>
     < /选择>

现在,您可以创建自定义标签栏,你的图像图标将在标签的中心。

I have created a tab in android which displays only images (does not contain any text ), I want the image to be displayed at the center of the tab .There are multiple tabs, each containing images, but the problem is that the image is not displayed at the center of each tab.

here is my xml

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@android:id/tabs"
             />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="#000000" >
        </TabWidget>
    </RelativeLayout>

</TabHost>

setting image in code:

newsTab.setIndicator("",getResources().getDrawable(R.drawable.news)).setContent(
                new Intent(RestauActivity.this, NewsActivity.class));

解决方案

1)- Create a layout file with name report_tabs.xml or any name you like.

In report_tabs use this code.

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/layout_tabsLayout"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/btn_selector"
     android:gravity="center"
     android:orientation="vertical"
     >

     <ImageView
     android:id="@+id/img_icon"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     android:layout_marginTop="3dp"
     />
    </LinearLayout>

2)- And use below code in your activity.

    Intent intent;
    tabhost = getTabHost();
    TabHost.TabSpec tabspec;
    intent = new Intent().setClass(getApplicationContext(),xxxxx.class);

    tabspec = tabhost.newTabSpec("First");
    view = LayoutInflater.from(this).inflate(R.layout.report_tabs,
tabhost.getTabWidget(), false);

    imgtabF = (ImageView) view.findViewById(R.id.img_icon);
    imgtabF.setBackgroundResource(R.drawable.tab_icon_selector);

    tabspec.setIndicator(view);
    tabspec.setContent(intent);
    tabhost.addTab(tabspec);

3)- Create a file with name tab_icon_selector in drawable for changing the icon on tab click like this:-

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false" 
          android:state_pressed="false" android:drawable="@drawable/medical_icon_unselect"    
     />
     <item android:state_focused="false" android:state_selected="true" 
     android:state_pressed="false" android:drawable="@drawable/medical_icon_sel" /> 
     <!-- Focused states -->
     <item android:state_focused="true" android:state_selected="false" 
     android:state_pressed="false" android:drawable="@drawable/medical_icon_sel" />
     <item android:state_focused="true" android:state_selected="true" 
     android:state_pressed="false" android:drawable="@drawable/medical_icon_sel" /> 
     <!-- Pressed -->
     <item android:state_pressed="true" android:drawable="@android:color/transparent"/> 
     </selector>

Now you can create your custom Tab bar and your image icon will be in center of the tab.

这篇关于如何在标签的中心展示形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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