android – ActionBar.Tab的自定义视图无法正确显示

前端之家收集整理的这篇文章主要介绍了android – ActionBar.Tab的自定义视图无法正确显示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前有一个使用选项卡式导航实现的actionBar,现在想要自定义选项卡.我为每个选项卡创建了一个自定义 XML布局,以获得textview和可单击的ImageButton,并使用ActionBar.Tab.setCustomView进行设置.问题是布局的所有属性都没有任何效果(例如对齐).任何帮助,将不胜感激!

这是一张显示产生的图像:@L_403_1@

我的自定义选项卡布局XML文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="horizontal" >
  6. <TextView
  7. android:id="@+id/tab_title"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:layout_centerHorizontal="true"
  11. android:layout_centerVertical="true"
  12. />
  13. <ImageButton
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_alignParentRight="true"
  17. android:layout_centerVertical="true"
  18. android:src="@drawable/ic_action_refresh"/>
  19.  
  20. </RelativeLayout>

ActionBar代码

  1. final ActionBar actionBar = getActionBar();
  2.  
  3. //Because there is no 'parent' when navigating from the action bar,the home button for the Action Bar is disabled.
  4. actionBar.setHomeButtonEnabled(false);
  5.  
  6. //Specify that the actionBar will include a tabbed navigation
  7. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  8.  
  9.  
  10. // Setting up the ViewPager rowingViewPager,attaching the RowingFragmentPagerAdapter and setting up a listener for when the
  11. // user swipes between sections.
  12. rowViewPager = (ViewPager) findViewById(R.id.pager);
  13. rowViewPager.setAdapter(rowAdapter);
  14. rowViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
  15. {
  16. @Override
  17. public void onPageSelected(int position)
  18. {
  19. actionBar.setSelectedNavigationItem(position);
  20. }
  21. });
  22.  
  23.  
  24. // For loop to add tabs for each fragment to the actionBar
  25. for (int tab_counter = 0; tab_counter < rowAdapter.getCount(); tab_counter++)
  26. {
  27. Tab new_tab = actionBar.newTab();
  28. new_tab.setCustomView(R.layout.custom_tab);
  29. TextView tab_title = (TextView) new_tab.getCustomView().findViewById(R.id.tab_title);
  30. tab_title.setText(rowAdapter.getTabTitle(tab_counter));
  31. new_tab.setTabListener(this);
  32. // Add tab with specified text as well as setting this activity as the TabListener.
  33. actionBar.addTab(new_tab);
  34.  
  35.  
  36. }
  37.  
  38. rowViewPager.setOffscreenPageLimit(2); //Sets the number of off-screen fragments to store and prevent re-load. Will increase if future fragments are added.
  39. }

解决方法

问题在于不是自定义actionBar布局本身,而是改变样式.默认的ActionBar有一个属性,左边和右边的填充是16dp.您可以在标签布局中执行任何操作,但不会覆盖此选项.

为此,我在ActionBar选项卡的styles.xml中编写了一个新样式,我在其中覆盖了该填充并将其应用于应用程序主题中的actionBarTabStyle属性.这是我的新styles.xml,它显示了这一变化.

  1. <resources>
  2.  
  3. <!--
  4. Base application theme,dependent on API level. This theme is replaced
  5. by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
  6. -->
  7. <style name="AppBaseTheme" parent="android:Theme.Light">
  8. <!--
  9. Theme customizations available in newer API levels can go in
  10. res/values-vXX/styles.xml,while customizations related to
  11. backward-compatibility can go here.
  12. -->
  13. </style>
  14.  
  15. <!-- Application theme. -->
  16. <style name="AppTheme" parent="AppBaseTheme">
  17. <!-- All customizations that are NOT specific to a particular API-level can go here. -->
  18. <item name="android:actionBarTabStyle">@style/ActionBarTabStyle</item>
  19. </style>
  20.  
  21.  
  22. <style name="ActionBarTabStyle" parent="@android:style/Widget.Holo.ActionBar.TabView">
  23. <item name="android:paddingLeft">0dp</item>
  24. <item name="android:paddingRight">0dp</item>
  25. </style>
  26.  
  27. </resources>

猜你在找的Android相关文章