2014-04-11 70 views
1

我相對較新的android,我真的想知道是否有可能自定義選項卡選擇如圖像(網址:http://i.stack.imgur.com/Hg5sT.png)。正如你在圖像中看到的,當選擇一個標籤時,會有一個向下的三角形形狀(圖像)。如果是的話,這怎麼能實現xml/java。我試着在選項卡中有一個背景圖像,但沒有按預期顯示。我在網上進行了很多研究,但無法找到如何做到這一點。Android自定義選項卡選擇指標與圖像

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

回答

1

如果您想達到這個目標,您必須爲單個選項卡創建兩個圖像。並改變他們的選擇

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

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

如果圖像將被home_selected選中,但在未選中狀態下圖像將被替換,您必須爲每個選項卡創建這兩個xml文件。你的問題將得到解決。

+0

它沒有解決我的問題,但給了我想法。我會接受這個答案 – user3346173