2010-12-16 14 views
7

मुझे अपनी टैबविड्ज पृष्ठभूमि छवियों को स्केल करने की आवश्यकता है ताकि वे पहलू अनुपात बनाए रखें।
मैं TabWidget के साथ एक टैबहोस्ट का उपयोग कर रहा हूं। मैं फिर छवियों को सेट करने के लिए setBackgroundDrawable का उपयोग कर रहा हूँ।टैबहोस्ट/टैबविड्ज - स्केल पृष्ठभूमि छवि?

मुझे यहां एक करीबी उत्तर मिला - Background in tab widget ignore scaling। हालांकि, मुझे यकीन नहीं है कि नया ड्रायबल कोड कहां जोड़ना है। (HelloTabWidget उदाहरण के साथ काम करते हुए, मेरे मॉड्यूल में से कोई भी रिलेवेटिवआउट का उपयोग नहीं करता है, और मुझे "टैबकंटेंट" के लिए कोई लेआउट दिखाई नहीं देता है।)

मुझे यह थ्रेड भी मिला - Android: Scale a Drawable or background image?। इसके अनुसार, ऐसा लगता है कि मुझे अपनी छवियों को पूर्व-स्केल करना होगा, जो उन्हें स्केल करने योग्य बनाने के पूरे उद्देश्य को हरा देता है।

मुझे एक और धागा भी मिला जहां किसी ने ड्रायबल क्लास को उप-वर्गीकृत किया ताकि यह स्केल न हो, या यह ठीक से स्केल करेगा। मुझे अब यह नहीं मिल रहा है, लेकिन ऐसा लगता है कि जब आप mTab.setScaleType (centerInside) जैसे कुछ सरल करने में सक्षम होना चाहिए।

यहाँ मेरी कोड है:

main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/main_background"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"/>   
    <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0"/> 
    </LinearLayout> 
</TabHost> 

मुख्य गतिविधि:

 tabHost.setOnTabChangedListener(new OnTabChangeListener() { 
      TabHost changedTabHost = getTabHost(); 
      TabWidget changedTabWidget = getTabWidget(); 
      View changedView = changedTabHost.getTabWidget().getChildAt(0); 

      public void onTabChanged(String tabId) { 
       int selectedTab = changedTabHost.getCurrentTab(); 
       TabWidget tw = getTabWidget(); 

       if(selectedTab == 0) { 
        //setTitle("Missions Timeline"); 
        View tempView = tabHost.getTabWidget().getChildAt(0); 
        tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_timeline_on)); 
        tempView = tabHost.getTabWidget().getChildAt(1); 
        tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_map_off)); 
        tempView = tabHost.getTabWidget().getChildAt(2); 
        tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_search_off)); 
        tempView = tabHost.getTabWidget().getChildAt(3); 
        tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_news_off)); 
        tempView = tabHost.getTabWidget().getChildAt(4); 
        tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_license_off)); 
          //ImageView iv = (ImageView)tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon); 
          //iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_timeline_on)); 
          //iv = (ImageView)tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon); 
          //iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_off)); 
        } else if (selectedTab == 1) { 
         //setTitle("Spinoffs Around You"); 
         View tempView = tabHost.getTabWidget().getChildAt(0); 
         tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_timeline_off)); 
         tempView = tabHost.getTabWidget().getChildAt(1); 
         tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_map_on)); 
         tempView = tabHost.getTabWidget().getChildAt(2); 
         tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_search_off)); 
         tempView = tabHost.getTabWidget().getChildAt(3); 
         tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_news_off)); 
         tempView = tabHost.getTabWidget().getChildAt(4); 
         tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_license_off)); 
} 

मैं भी छवियों 9patch कोशिश की, लेकिन वे बहुत छोटा किया जा रहा है हवा।

तो, इस बारे में जाने का सबसे अच्छा तरीका क्या है?

उत्तर

0

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

आपका यूआई लेआउट इस तरह से डिज़ाइन किया जाना चाहिए कि किसी संसाधन-आधारित पृष्ठभूमि छवियों को डिवाइस के घनत्व/स्क्रीन आकार के लिए सही समाधान के लिए पूर्व-स्केल किया जाना चाहिए। दूसरे शब्दों में आपको Supporting Multiple Screens देव मार्गदर्शिका में उल्लिखित संसाधन फ़ोल्डर नामकरण सम्मेलन का उपयोग करके एकाधिक स्क्रीन घनत्व और आकारों को पूरा करने के लिए प्रत्येक ड्रायबल के कई संस्करण प्रदान करना चाहिए।

यूआई को तब रखा जाना चाहिए कि डिवाइस के बीच स्क्रीन आकार के किसी भी मामूली अंतर को उसके निहित विचारों की पृष्ठभूमि छवियों को स्केल करने की आवश्यकता के बिना संभाला जा सके। जाहिर है, मुझे नहीं पता कि आपका प्रस्तावित यूआई कैसा दिखता है, इसलिए कोई ठोस सुझाव बनाना मुश्किल है, लेकिन आमतौर पर मैं दृश्य पृष्ठभूमि के बीच स्थान को गतिशील रूप से भरने के लिए सरल पृष्ठभूमि ब्लॉक रंग या ShapeDrawable एस का उपयोग करता हूं।

हालांकि, अगर आप वास्तव में सच में हैं कि आप उपरोक्त प्रचार के बावजूद अपनी पृष्ठभूमि छवियों को स्केल करना चाहते हैं :-), अपने मौजूदा ड्रॉइंग को लपेटने के लिए ScaleDrawable का उपयोग क्यों न करें?

आप ऊंचाई और दोनों अपने दृष्टिकोण को और पृष्ठभूमि छवि drawable की चौड़ाई पता है, तो आप की तरह कुछ कर सकते हैं:

Drawable backgroundDrawable = getResources().getDrawable(R.drawable.tab_license_off); 
tempView.setBackgroundDrawable(
    new ScaleDrawable(
     backgroundDrawable, 
     Gravity.CENTER, 
     tempView.getWidth()/backgroundDrawable.getIntrinsicWidth(), 
     tempView.getHeight()/backgroundDrawable.getIntrinsicHeight()); 
1

चेक इस बाहर और मुझे पता है कि क्या यह आप के लिए काम किया करते हैं।

Custom Android Tab

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