简体   繁体   English

带有图像的Android自定义标签选择指示器

[英]Android custom tab selection indicator with an image

I am relatively new to android, and I was really wondering if it is possible to customize tab selection as in the image( URL: http://i.stack.imgur.com/Hg5sT.png ) . 我是android的新手,我真的很想知道是否可以像在图片中那样自定义选项卡选择(URL: http : //i.stack.imgur.com/Hg5sT.png )。 As you see in the image, there is a little triangular shape(an image) pointing downward when a tab is selected. 如您在图像中看到的,当选择一个选项卡时,有一个小的三角形形状(图像)指向下方。 If yes, how can this be achieved xml/java. 如果是,如何在xml / java中实现。 I tried having a background image in the tab but it didn't appear as expected. 我尝试在标签中添加背景图片,但未按预期显示。 I researched a lot online, but couldn't find how to do this. 我在网上做了很多研究,但找不到如何做。

Thank you for your patience and cooperation. 感谢您的耐心配合。 http://i.stack.imgur.com/Hg5sT.png http://i.stack.imgur.com/Hg5sT.png

If you want to achieve this you must create two images for a single tab . 如果要实现此目的,则必须为单个选项卡创建两个图像。 And change them on selection 并在选择时进行更改

MainActivity 主要活动

public class MainActivity extends TabActivity
{
    TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        tabHost = getTabHost();

        TabSpec spec;

        Intent intent;

        //Home Tab
        View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.home, null);  //  R.layout.home  is the home.xml where you have entered two image 

        intent = new Intent(MainActivity.this, Firstclass.class);

        spec = tabHost.newTabSpec("HOME").setIndicator(view1)
                .setContent(intent);

        tabHost.addTab(spec);

        //Calendar Tab
        View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.calendar_tab, null);

        intent = new Intent(MainActivity.this, Calendar.class);

        spec = tabHost.newTabSpec("CALENDAR").setIndicator(view2)
                .setContent(intent);

        tabHost.addTab(spec);

Home.xml Home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/home_tab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        android:layout_centerInParent="true"
        android:background="@drawable/selector1" />

</RelativeLayout>

@drawable/selector1 @绘制/ selector1

selector1.xml selector1.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/home_selected" android:state_selected="true"></item>
    <item android:drawable="@drawable/home_unselect" android:state_selected="false"></item>

</selector>

on state_selected the image wil be home_selected but on unselected state the image will be replaced you have to create these two xml files for each tab. 在state_selected上,图像将被home_selected选中,但是在未选中状态下,图像将被替换,您必须为每个选项卡创建这两个xml文件。 And your problem will be solved. 您的问题将得到解决。

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

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