2012-04-03 21 views
6

पर टैब जैसे टैब बनाने के लिए कैसे करें ताकि एंड्रॉइड हर किसी के उपयोग के लिए इस अच्छी UI मार्गदर्शिका को बनाने के अपने रास्ते से बाहर हो जाए। लेकिन मुझे कहीं भी नहीं दिखाई देता है जहां यह इन तत्वों को बनाने के तरीके के कोड उदाहरण दिखाता है।एंड्रॉइड: एंड्रॉइड यूआई पेज

टैब के लिए यूआई दिशानिर्देश यहां पाए जा सकते हैं। http://developer.android.com/design/building-blocks/tabs.html

क्या कोई जानता है कि टैब बनाने के तरीके को यह कैसे पसंद है? enter image description here

किसी भी मदद की सराहना की जाएगी, धन्यवाद।

समाधान पोस्ट किया गया
ठीक है, तो यहां कुछ अच्छा दिखने वाले टैब बनाने की कोशिश कर रहे लगभग 10 घंटे बर्बाद होने के बाद मैं ऐसा कर रहा हूं।
enter image description here

सबसे पहले मैंने टैब के एंड्रॉइड के कार्यान्वयन का उपयोग करने के पूरे विचार को तोड़ दिया। एक कारण से टैब होस्ट विजेट को एक्शन बार के लिए बहिष्कृत करने का अनुमान है, लेकिन एक्शन बार केवल एंड्रॉइड 3 से काम करता है।

मुझे अंत में पता चला कि यदि रैखिक लेआउट के लिए एक रैखिक लेआउट और पृष्ठभूमि के रूप में उपयोग किया जाता है तो मैंने उस छवि को रखा जिसे मैं उपयोग करना चाहता था (9 पैच छवि का उपयोग करके)। फिर उस लाइनरलेआउट के शीर्ष पर टेक्स्ट डालने के लिए एक और लाइनरलेआउट और टेक्स्टव्यू बनाएं। फिर अपने रैखिक लेआउट क्लिक करने योग्य बनाओ। फिर जब आप अधिक उन्नत हो जाते हैं तो आप रैखिक लेआउट पृष्ठभूमि को एक एक्सएमएल चयनकर्ता बना सकते हैं और आप जाने के लिए अच्छे हैं। हालांकि आपको यह सब कुछ नहीं मिला है जो मेरा कोड है।

LinearLayout

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="50dp" 
    android:background="@color/main_screen_bg_color" 
    android:orientation="horizontal" 
    android:padding="2dp" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="@drawable/selector_not_current" 
     android:clickable="true" 
     android:onClick="onClickSub" 
     android:orientation="horizontal" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:orientation="vertical" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:singleLine="true" 
       android:text="Example 1" 
       android:textColor="@color/black" 
       android:textSize="18sp" 
       android:textStyle="bold" /> 
     </LinearLayout> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="@drawable/selector_current" 
     android:clickable="true" 
     android:onClick="onClickFoodDetails" 
     android:orientation="horizontal" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:orientation="vertical" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:singleLine="true" 
       android:text="Example 2" 
       android:textColor="@color/black" 
       android:textSize="18sp" 
       android:textStyle="bold" /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

उदाहरण चयनकर्ता

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true" 
     android:drawable="@drawable/selected_pressed_tab" /> <!-- pressed --> 
<item android:state_focused="true" 
     android:drawable="@drawable/selected_pressed_tab" /> <!-- focused --> 
<item android:drawable="@drawable/selected_tab" /> <!-- default --> 

आशा इस हर किसी को मदद मिलती है। एंड्रॉइड टैब काम करने के लिए बहुत परेशान थे, क्योंकि यह सिर्फ खुद को खरोंच से बनाना आसान था। शुभ लाभ!

+1

AFAIK, यह Holo विषय का उपयोग कर एंड्रॉयड आईसीएस के डिफ़ॉल्ट दिया गया है। – Ghost

उत्तर

5

ऐसा कुछ करें।

यह एक पूर्ण कार्य कोड है।गतिविधि की OnCreate विधि Tabactivity

tabHost = getTabHost(); 
    Intent intent; 
    intent = new Intent().setClass(this, FirstActvity.class); 
    setupTab("NearBy", intent, R.drawable.firsttabdrawable); 
    intent = new Intent().setClass(this, SecondActivity.class); 
    setupTab("History", intent, R.drawable.secondtabdrawable); 
    intent = new Intent().setClass(this, ThirdActivity.class); 
    setupTab("Setting", intent, R.drawable.thirdtabdrawable); 

विस्तार में

कहीं का आनंद के रूप में setupTab पद्धतियां निर्धारित

private void setupTab(String tag, Intent intent, int selectorId) { 
    View tabView = LayoutInflater.from(tabHost.getContext()).inflate(R.layout.view, null); 
    tabView.setBackgroundResource(selectorId); 
    TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabView).setContent(intent); 
    tabHost.addTab(setContent); 
    } 

view.xml रूप

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

और के रूप में drawable फ़ोल्डर में firsttabdrawable.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- When selected, use grey --> 
    <item android:drawable="@drawable/selectedfirsttabimage" 
     android:state_selected="true" /> 
    <!-- When not selected, use white--> 
    <item android:drawable="@drawable/notselectedfirsttabimage" /> 
</selector> 

उसी तरह से secondtabdrawable.xml और thirddrawable.xml परिभाषित

+0

बस अपने डिजाइनर से उपरोक्त टैब – Javanator

+0

के लिए 6 छवियों को काटने के लिए कहें यह काम करता प्रतीत होता है, मैंने लगभग इसे सही स्थापित किया है। जब आप अपने टैब आइकन डिज़ाइन करते हैं तो आप किस अनुपात का उपयोग करते हैं? आप किस पिक्सेल एक्स पिक्सेल का उपयोग करते हैं? स्क्रीन शॉट से मुझे 120 पिक्सेल एक्स 48 पिक्सल मिले। क्या वो सही है? –

2

आपको आवश्यक टैब एक्शनबार का हिस्सा हैं। विशेष रूप से वे तब प्रदर्शित होते हैं जब एक्शनबार नेविगेशन टैब मोड में होता है।

http://developer.android.com/guide/topics/ui/actionbar.html (के तहत "नेविगेशन टैब जोड़ना" देखें)

आप जो एक पुस्तकालय है कि आप एंड्रॉयड के लगभग सभी संस्करणों पर ActionBar दे देंगे www.ActionbarSherlock.com उपयोग कर सकते हैं। यह आधिकारिक के समान काम करता है, और टैब भी शामिल है।

टैबएक्टिविटी का अब और उपयोग न करें, यह पुराना है और बहिष्कृत किया जा रहा है। एक्शनबार भविष्य है।

संबंधित मुद्दे