2013-11-21 2 views
5

Ich verwende die ActionBar und ich möchte eine benutzerdefinierte View in den Navigationsregisterkarten festlegen.Wie stelle ich eine benutzerdefinierte Ansicht in den Navigations-Tabs von ActionBar ein und lasse die Tabs an ihre Höhe anpassen?

Die Höhe der Registerkarten scheint fest zu sein, und meine benutzerdefinierte View ist größer, so dass es nicht passt.

Ich versuche, den Stil zu gestalten, wie unten dargestellt, aber es macht nicht die Laschen höher ...

Wie kann ich die Höhe der Registerkarten zu meiner benutzerdefinierten View Größe anpassen?

(Ich weiß, ich Tab ‚s setIcon und setTitle in meinem Fall verwenden könnte, aber ich will noch einen benutzerdefinierten verwenden View)

enter image description here

styles.xml

<resources> 
    <style name="AppBaseTheme" parent="android:Theme.Holo"> 
    </style> 
    <style name="AppTheme" parent="AppBaseTheme"> 
     <item name="android:actionBarTabStyle">@style/MyTabStyle</item> 
    </style> 

    <style name="MyTabStyle" parent="@android:Widget.ActionBar.TabView"> 
     <item name="android:height">85dp</item> 
    </style> 
</resources> 

custom_tab.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:gravity="center_horizontal" > 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="60dp" 
     android:layout_height="60dp" 
     android:scaleType="centerInside" /> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:maxLines="1" /> 

</LinearLayout> 

MainActivity.java

public class MainActivity extends FragmentActivity implements 
ActionBar.TabListener { 

    SectionsPagerAdapter mSectionsPagerAdapter; 

    ViewPager mViewPager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Set up the action bar. 
     final ActionBar actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the app. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(
       getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     // When swiping between different sections, select the corresponding 
     // tab. We can also use ActionBar.Tab#select() to do this if we have 
     // a reference to the Tab. 
     mViewPager 
     .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
      @Override 
      public void onPageSelected(int position) { 
       actionBar.setSelectedNavigationItem(position); 
      } 
     }); 

     // For each of the sections in the app, add a tab to the action bar. 
     for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 
      // Create a tab with text corresponding to the page title defined by 
      // the adapter. Also specify this Activity object, which implements 
      // the TabListener interface, as the callback (listener) for when 
      // this tab is selected. 

      LinearLayout view = (LinearLayout) getLayoutInflater().inflate(R.layout.custom_tab, null); 

      ImageView icon = (ImageView) view.findViewById(R.id.icon); 
      icon.setImageResource(R.drawable.about); 

      TextView title = (TextView) view.findViewById(R.id.title); 
      title.setText("About"); 


      actionBar.addTab(actionBar.newTab() 
        //.setText(mSectionsPagerAdapter.getPageTitle(i)) 
        .setCustomView(view) 
        .setTabListener(this)); 
     } 
    } 

    /*****/ 
} 

Antwort

0

hält dies die ActionBar auf der Spitze, aber ich weiß nicht, warum!

ActionBar.DISPLAY_SHOW_HOME 

der ActionBar Aufenthalt in der Spitze:

actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME); 

die gehen Registerkarten am oberen Rand des ActionBar:

actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM 
0

AFAIK, können Sie nicht die Höhe der Aktion ändern Bar-Registerkarten. Aber Sie können das beabsichtigte Verhalten erreichen, indem Sie einen TabHost zusammen mit einem ViewPager verwenden. Auf diese Weise können Sie Ihren TabIndicators benutzerdefinierte Ansichten hinzufügen und haben auch die Swiping-Funktion.

Verwandte Themen