2013-11-03 8 views
6

मेरे पास वर्तमान में टैबबंद नेविगेशन के साथ कार्यान्वित एक क्रियाबार है, और अब टैब को स्वयं कस्टमाइज़ करना चाहते हैं। मैंने प्रत्येक टैब के लिए एक टेक्स्टव्यू और एक क्लिक करने योग्य छवि बटन रखने के लिए एक कस्टम एक्सएमएल लेआउट बनाया है, और इसे सेट करने के लिए ActionBar.Tab.setCustomView का उपयोग किया है। समस्या यह है कि लेआउट के लिए कोई भी गुण किसी भी प्रभाव (जैसे संरेखण) प्रतीत होता है। किसी भी सहायता की सराहना की जाएगी!एक्शनबार के लिए कस्टम व्यू। सही ढंग से प्रदर्शित नहीं हो रहा है

यहाँ दिखा क्या उत्पादन किया है एक छवि है: Image of what is being produced

मेरे कस्टम टैब लेआउट एक्सएमएल फ़ाइल

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" > 
<TextView 
    android:id="@+id/tab_title" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    /> 
<ImageButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:src="@drawable/ic_action_refresh"/> 

</RelativeLayout> 

ActionBar कोड:

final ActionBar actionBar = getActionBar(); 

      //Because there is no 'parent' when navigating from the action bar, the home button for the Action Bar is disabled.  
      actionBar.setHomeButtonEnabled(false); 

      //Specify that the actionBar will include a tabbed navigation 
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 


     // Setting up the ViewPager rowingViewPager, attaching the RowingFragmentPagerAdapter and setting up a listener for when the 
     // user swipes between sections. 
      rowViewPager = (ViewPager) findViewById(R.id.pager); 
      rowViewPager.setAdapter(rowAdapter); 
      rowViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() 
      { 
       @Override 
       public void onPageSelected(int position) 
       { 
        actionBar.setSelectedNavigationItem(position); 
       } 
      }); 


     // For loop to add tabs for each fragment to the actionBar 
      for (int tab_counter = 0; tab_counter < rowAdapter.getCount(); tab_counter++) 
      { 
       Tab new_tab = actionBar.newTab(); 
       new_tab.setCustomView(R.layout.custom_tab); 
       TextView tab_title = (TextView) new_tab.getCustomView().findViewById(R.id.tab_title); 
       tab_title.setText(rowAdapter.getTabTitle(tab_counter)); 
       new_tab.setTabListener(this); 
       // Add tab with specified text as well as setting this activity as the TabListener. 
       actionBar.addTab(new_tab); 


      } 

     rowViewPager.setOffscreenPageLimit(2); //Sets the number of off-screen fragments to store and prevent re-load. Will increase if future fragments are added. 
    } 
+0

बड़ा सवाल है, लेकिन मैं हो रही है अशक्त के लिए इस लाइन पर सूचक अपवाद। TextView tab_title = (TextView) new_tab.getCustomView()। FindViewById (R.id.tab_title); कोई मदद? – dsharew

उत्तर

6

समस्या ActionBar लेआउट खुद को अनुकूलित नहीं में निहित है , बल्कि शैली बदलना। डिफ़ॉल्ट एक्शनबार में एक ऐसी संपत्ति होती है जहां बाएं और दाएं पैडिंग 16 डीपी होती है। आप अपने टैब लेआउट में जो भी चाहें कर सकते हैं, लेकिन यह इसे ओवरराइड नहीं करेगा।

ऐसा करने के लिए, मैंने एक्शनबार टैब के लिए शैलियों.एक्सएमएल में एक नई शैली लिखी, जहां मैंने उस पैडिंग को ओवरराइड किया और इसे एप्लिकेशन थीम में क्रियाबर्ट स्टाइल विशेषता पर लागू किया। यहां मेरी नई शैलियों.एक्सएमएल है जो परिवर्तन दिखाती है।

<resources> 

    <!-- 
     Base application theme, dependent on API level. This theme is replaced 
     by AppBaseTheme from res/values-vXX/styles.xml on newer devices. 
    --> 
    <style name="AppBaseTheme" parent="android:Theme.Light"> 
     <!-- 
      Theme customizations available in newer API levels can go in 
      res/values-vXX/styles.xml, while customizations related to 
      backward-compatibility can go here. 
     --> 
    </style> 

    <!-- Application theme. --> 
    <style name="AppTheme" parent="AppBaseTheme"> 
     <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
     <item name="android:actionBarTabStyle">@style/ActionBarTabStyle</item> 
    </style> 


    <style name="ActionBarTabStyle" parent="@android:style/Widget.Holo.ActionBar.TabView"> 
    <item name="android:paddingLeft">0dp</item> 
    <item name="android:paddingRight">0dp</item> 
</style> 

</resources> 
0

इसका बहुत सरल सिर्फ अपने कस्टम दृश्य से imageView को हटाने और/drawable शीर्ष/बाएं/नीचे के रूप में जोड़ना सही TextView

यानी

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:drawableTop="@drawable/your_img" 
    android:text="Tab1" 
    /> 
संबंधित मुद्दे

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