如何在android中删除Tabwidget中的填充或边距?

前端之家收集整理的这篇文章主要介绍了如何在android中删除Tabwidget中的填充或边距?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想创建选项卡式应用程序.
一切都很好,但是当我创建标签时.标签之间的空间太大了.
我想删除这个填充或边距,但我不知道我该怎么做.
有什么建议吗?

XML

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@android:id/tabhost"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <LinearLayout
  7. android:orientation="vertical"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent">
  10.  
  11. <HorizontalScrollView
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:fillViewport="true"
  15. android:scrollbars="none" >
  16. <TabWidget
  17. android:id="@android:id/tabs"
  18. android:padding="0px"
  19. android:layout_margin="0px"
  20.  
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content">
  23.  
  24. <TextView
  25. android:tag="tab0"
  26. android:text="Tab 1"
  27. android:padding="0px"
  28. android:layout_margin="0px"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31.  
  32. />
  33. <TextView
  34. android:tag="tab1"
  35. android:text="Tab 2"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. />
  39. <TextView
  40. android:tag="tab2"
  41. android:text="Tab 3"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. />
  45. <TextView
  46. android:tag="tab3"
  47. android:text="Tab 4"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. />
  51. <TextView
  52. android:tag="tab4"
  53. android:text="Tab 5"
  54. android:layout_width="wrap_content"
  55. android:layout_height="wrap_content"
  56. />
  57. <TextView
  58. android:tag="tab5"
  59. android:text="Tab 6"
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. />
  63. <TextView
  64. android:tag="tab6"
  65. android:text="Tab 7"
  66. android:layout_width="wrap_content"
  67. android:layout_height="wrap_content"
  68. />
  69. <TextView
  70. android:tag="tab7"
  71. android:text="Tab 8"
  72. android:layout_width="wrap_content"
  73. android:layout_height="wrap_content"
  74. />
  75. <TextView
  76. android:tag="tab8"
  77. android:text="Tab 9"
  78. android:layout_width="wrap_content"
  79. android:layout_height="wrap_content"
  80. />
  81. <TextView
  82. android:tag="tab9"
  83. android:text="Tab 10"
  84. android:layout_width="wrap_content"
  85. android:layout_height="wrap_content"
  86. />
  87.  
  88. </TabWidget>
  89. </HorizontalScrollView>
  90.  
  91. <FrameLayout
  92. android:id="@android:id/tabcontent"
  93. android:layout_width="fill_parent"
  94. android:layout_height="fill_parent">
  95.  
  96. <TextView
  97. android:text="Hallo1"
  98. android:layout_width="fill_parent"
  99. android:layout_height="fill_parent" />
  100. <TextView
  101. android:text="Hallo2"
  102. android:layout_width="fill_parent"
  103. android:layout_height="fill_parent" />
  104. <TextView
  105. android:text="Hallo3"
  106. android:layout_width="fill_parent"
  107. android:layout_height="fill_parent" />
  108. <TextView
  109. android:text="Hallo4"
  110. android:layout_width="fill_parent"
  111. android:layout_height="fill_parent" />
  112. <TextView
  113. android:text="Hallo5"
  114. android:layout_width="fill_parent"
  115. android:layout_height="fill_parent" />
  116. <TextView
  117. android:text="Hallo6"
  118. android:layout_width="fill_parent"
  119. android:layout_height="fill_parent" />
  120. <TextView
  121. android:text="Hallo7"
  122. android:layout_width="fill_parent"
  123. android:layout_height="fill_parent" />
  124. <TextView
  125. android:text="Hallo8"
  126. android:layout_width="fill_parent"
  127. android:layout_height="fill_parent" />
  128. <TextView
  129. android:text="Hallo9"
  130. android:layout_width="fill_parent"
  131. android:layout_height="fill_parent" />
  132. <TextView
  133. android:text="Hallo10"
  134. android:layout_width="fill_parent"
  135. android:layout_height="fill_parent" />
  136.  
  137. </FrameLayout>
  138. </LinearLayout>
  139. </TabHost>

这是我的代码

  1. public class MainActivity extends Activity {
  2.  
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6.  
  7. setContentView(R.layout.activity_main);
  8.  
  9. TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
  10. tabHost.setup();
  11.  
  12. final TabWidget tabWidget = tabHost.getTabWidget();
  13. final FrameLayout tabContent = tabHost.getTabContentView();
  14. tabHost.getTabWidget().setDividerDrawable(R.drawable.empty);
  15.  
  16. // Get the original tab textviews and remove them from the viewgroup.
  17. TextView[] originalTextViews = new TextView[tabWidget.getTabCount()];
  18. for (int index = 0; index < tabWidget.getTabCount(); index++) {
  19. originalTextViews[index] = (TextView) tabWidget.getChildTabViewAt(index);
  20. }
  21. tabWidget.removeAllViews();
  22.  
  23. // Ensure that all tab content childs are not visible at startup.
  24. for (int index = 0; index < tabContent.getChildCount(); index++) {
  25. tabContent.getChildAt(index).setVisibility(View.GONE);
  26. }
  27.  
  28. // Create the tabspec based on the textview childs in the xml file.
  29. // Or create simple tabspec instances in any other way...
  30. for (int index = 0; index < originalTextViews.length; index++) {
  31. final TextView tabWidgetTextView = originalTextViews[index];
  32. final View tabContentView = tabContent.getChildAt(index);
  33.  
  34. TabSpec tabSpec = tabHost.newTabSpec((String) tabWidgetTextView.getTag());
  35. tabSpec.setContent(new TabHost.TabContentFactory() {
  36. @Override
  37. public View createTabContent(String tag) {
  38. return tabContentView;
  39. }
  40. });
  41. if (tabWidgetTextView.getBackground() == null) {
  42. tabSpec.setIndicator(tabWidgetTextView.getText());
  43. } else {
  44. tabSpec.setIndicator(tabWidgetTextView.getText(),tabWidgetTextView.getBackground());
  45. }
  46. tabHost.addTab(tabSpec);
  47. }
  48.  
  49.  
  50. tabHost.getTabWidget().setDividerDrawable(R.drawable.empty);
  51.  
  52. if (Integer.parseInt(Build.VERSION.SDK) >= Build.VERSION_CODES.HONEYCOMB) {
  53. tabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
  54. }
  55.  
  56. // tabHost.setCurrentTab(0);
  57. }
  58. }

解决方法

如果你看一下Tablayout的基本风格:
  1. <style name="Base.Widget.Design.TabLayout" parent="android:Widget">
  2. <item name="tabMaxWidth">@dimen/tab_max_width</item>
  3. <item name="tabIndicatorColor">?attr/colorAccent</item>
  4. <item name="tabIndicatorHeight">2dp</item>
  5. <item name="tabPaddingStart">12dp</item>
  6. <item name="tabPaddingEnd">12dp</item>
  7. <item name="tabBackground">?attr/selectableItemBackground</item>
  8. <item name="tabTextAppearance">@style/TextAppearance.Design.Tab</item>
  9. <item name="tabSelectedTextColor">?android:textColorPrimary</item>
  10. </style>

你看到这两行

  1. <item name="tabPaddingStart">12dp</item>
  2. <item name="tabPaddingEnd">12dp</item>

所以只需为你的Tablayout创建一个样式,如下所示:

  1. <style name="tab_bar">
  2. <item name="android:layout_width">match_parent</item>
  3. <item name="android:layout_height">65dp</item>
  4. <item name="android:background">@color/backgroundColor</item>
  5. <item name="android:tabStripEnabled">false</item>
  6. <item name="tabPaddingStart">0dp</item>
  7. <item name="tabPaddingEnd">0dp</item>
  8. </style>

并使用风格:

  1. <android.support.design.widget.TabLayout android:id="@+id/tabs"
  2. app:tabGravity="fill"
  3. app:tabMode="fixed"
  4. style="@style/tab_bar"/>

猜你在找的Android相关文章