簡體   English   中英

帶有圖像的Android自定義標簽選擇指示器

[英]Android custom tab selection indicator with an image

我是android的新手,我真的很想知道是否可以像在圖片中那樣自定義選項卡選擇(URL: http : //i.stack.imgur.com/Hg5sT.png )。 如您在圖像中看到的,當選擇一個選項卡時,有一個小的三角形形狀(圖像)指向下方。 如果是,如何在xml / java中實現。 我嘗試在標簽中添加背景圖片,但未按預期顯示。 我在網上做了很多研究,但找不到如何做。

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

如果要實現此目的,則必須為單個選項卡創建兩個圖像。 並在選擇時進行更改

主要活動

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

<?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>

@繪制/ 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文件。 您的問題將得到解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM