【问题标题】:Android custom tab selection indicator with an image带有图像的 Android 自定义选项卡选择指示器
【发布时间】:2014-04-14 14:52:19
【问题描述】:

我对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.如果是,如何实现xml/java。我尝试在选项卡中添加背景图像,但没有按预期显示。我在网上研究了很多,但找不到如何做到这一点。

感谢您的耐心配合。 http://i.stack.imgur.com/Hg5sT.png

【问题讨论】:

    标签: android android-layout android-tabhost


    【解决方案1】:

    如果你想实现这一点,你必须为一个标签创建两个图像。并在选择时更改它们

    主活动

    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);
    

    主页.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.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>
    

    在 state_selected 上,图像将是 home_selected,但在未选择状态下,图像将被替换,您必须为每个选项卡创建这两个 xml 文件。你的问题就会迎刃而解。

    【讨论】:

    • 它没有解决我的问题,但给了我一些想法。我会接受这个答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    相关资源
    最近更新 更多