在Android中更改选项卡中的文本大小

6

我一直面临着选项卡中文本大小的问题。下面是图片:

enter image description here

这是我的代码:

MainActivity.java

public class MainActivity extends Activity implements OnTabChangeListener, OnPageChangeListener{  
    private TabHost tabHost;  
    private ViewPager pager; 

 @Override  
 public void onCreate(Bundle savedInstanceState) {  
   super.onCreate(savedInstanceState);  
   setContentView(R.layout.activity_main);  
   tabHost = (TabHost)findViewById(android.R.id.tabhost);  
   pager = (ViewPager) findViewById(R.id.pager);  

   tabHost.setup();  
   TabWidget tabwidget=tabHost.getTabWidget();

   TabSpec spec = tabHost.newTabSpec("tab1");  
   spec.setContent(R.id.tab1);  
   spec.setIndicator("Check In");  
   tabHost.addTab(spec);  

   spec = tabHost.newTabSpec("tab2");  
   spec.setContent(R.id.tab2);  
   spec.setIndicator("Buddies");  
   tabHost.addTab(spec);  

   spec = tabHost.newTabSpec("tab3");  
   spec.setContent(R.id.tab3);  
   spec.setIndicator("Recommendation");  
   tabHost.addTab(spec); 

   spec = tabHost.newTabSpec("tab4");  
   spec.setContent(R.id.tab4);  
   spec.setIndicator("Feed");  
   tabHost.addTab(spec); 

   spec = tabHost.newTabSpec("tab5");  
   spec.setContent(R.id.tab5);  
   spec.setIndicator("Last");  
   tabHost.addTab(spec);

   pager.setAdapter(new MyPagerAdapter(this));  
   pager.setOnPageChangeListener(this);  
   tabHost.setOnTabChangedListener(this);  

 }  
    @Override  
    public void onTabChanged(String tabId){  
         int pageNumber = 0;  
         if(tabId.equals("tab1"))
         {  
              pageNumber = 0;  

         }

         else if(tabId.equals("tab2"))
         {  
              pageNumber = 1;

         }

         else if(tabId.equals("tab3"))
         {  
              pageNumber = 2;  

         }

         else if(tabId.equals("tab4"))
         {  
              pageNumber = 3;  


         }

         else if(tabId.equals("tab5"))
         {  
              pageNumber = 4;  

         }
         else
         {  
              pageNumber = 0;  

         }  

         pager.setCurrentItem(pageNumber);  
    } 

    @Override  
    public void onPageSelected(int pageNumber) {  
         tabHost.setCurrentTab(pageNumber); 

    }
    @Override
    public void onPageScrollStateChanged(int arg0) {


    }
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {


    }
    }

activity_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="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <FrameLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />

            <FrameLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />

            <FrameLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />

            <FrameLayout
                android:id="@+id/tab4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />

            <FrameLayout
                android:id="@+id/tab5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />
        </FrameLayout>

        <android.support.v4.view.ViewPager
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</TabHost>

我希望整段文本可以设置在每个选项卡中,该怎么做?

我一直在尝试使用以下代码更改文本大小,并得到了以下结果:

代码

 TextView x = (TextView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
   x.setTextSize(10);
   tabHost.getTabWidget().getChildAt(0).getLayoutParams().width = 100;

   TextView x1 = (TextView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.title);
   x1.setTextSize(10);
   tabHost.getTabWidget().getChildAt(1).getLayoutParams().width = 90;

   TextView x2 = (TextView) tabHost.getTabWidget().getChildAt(2).findViewById(android.R.id.title);
   x2.setTextSize(10);
   tabHost.getTabWidget().getChildAt(2).getLayoutParams().width = 100;

   TextView x3 = (TextView) tabHost.getTabWidget().getChildAt(3).findViewById(android.R.id.title);
   x3.setTextSize(10);
   tabHost.getTabWidget().getChildAt(3).getLayoutParams().width = 40;

   TextView x4 = (TextView) tabHost.getTabWidget().getChildAt(4).findViewById(android.R.id.title);
   x4.setTextSize(10);
   tabHost.getTabWidget().getChildAt(4).getLayoutParams().width = 40;

结果的图片

在此输入图像描述

但是我希望输出的结果如下图所示

在此输入图像描述

根据上面的图片,我的标签小部件应该是可移动的,我该怎么做?


你尝试过从这个库中使用 shouldExpand 吗?https://github.com/LordZoltan/PagerSlidingTabStrip.Net/wiki/Using-and-customising-PagerSlidingTabStrip.Net - Radu
9个回答

4
您可以使用TabLayout和ViewPager,将tabMode设置为scrollable,代码如下:
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="center"
        app:tabMode="scrollable" />

4

我们如何更改每个选项卡的宽度? - thumber nirmal
2
tabHost.getTabWidget().getChildAt(0).getLayoutParams().width = 50; - Dinesh Raj
把以下与编程有关的内容从英语翻译成中文。只返回翻译后的文本:对不起,我没有看到您编辑后的问题。请提供需要翻译的完整文本。 - thumber nirmal

2
 app:tabMode="scrollable"
 app:tabGravity="fill"

在您的选项卡XML中使用此行,它将完成此操作。

0
<style name="MyTabLayout" parent="Base.Widget.Design.TabLayout">
    <item name="tabTextAppearance">@style/MyTabTextAppearance</item>

<style name="MyTabTextAppearance" parent="TextAppearance.AppCompat.Button">
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="textAllCaps">true</item>

<com.google.android.material.tabs.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/welcome_menu_icon_height_10"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="@dimen/margin_15"
    android:background="@android:color/transparent"
    android:nestedScrollingEnabled="true"
    android:scrollbarThumbVertical="@color/white"
    android:textAlignment="center"
    app:tabGravity="center"
    app:tabIndicatorAnimationDuration="1"
    app:tabIndicatorColor="@color/white"
    app:tabMode="scrollable"
    app:tabSelectedTextColor="@color/white"
    app:tabTextAppearance="@style/MyTabTextAppearance"
    app:tabTextColor="@color/white" />

 tabLayout.addTab(tabLayout.newTab().setText(R.string.casa))
    tabLayout.addTab(tabLayout.newTab().setText(R.string.fdr_dps))
    tabLayout.addTab(tabLayout.newTab().setText(R.string.loan))

你可以试一下,希望它能够正常工作。


0

很容易做,只需在您的xml文件中添加样式并声明所需的大小即可。

<style name="CustomTheme" parent="@android:style/Theme">
    <item name="android:tabWidgetStyle">@style/CustomTabWidget</item>
</style>
<style name="CustomTabWidget" parent="@android:style/Widget.TabWidget">
    <item name="android:textAppearance">@style/CustomTabWidgetText</item>
</style>
<style name="CustomTabWidgetText" 
    parent="@android:style/TextAppearance.Widget.TabWidget">
    <item name="android:textSize">100sp</item>
    <item name="android:textStyle">**strong text**</item>
</style>

不要忘记在manifest文件中添加带有样式名称的主题。

0

你也可以自定义你的选项卡的文本和背景。查看这篇博客,了解如何做到这一点的非常好的教程


0

只需将TabLayout放在HorizontalScrollView中:

<HorizontalScrollView
             android:layout_width="match_parent"
             android:layout_height="wrap_content">
                <android.support.design.widget.TabLayout
                 android:id="@+id/tabs"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content" />
    </HorizontalScrollView>

0
<android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"/>

使用 app:tabMode="scrollable"


-1
<TabHost
     android:id="@+id/tabhost"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_alignParentTop="true"
     android:layout_alignParentLeft="true">

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical" >

         <TabWidget
             android:id="@android:id/tabs"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             >

         </TabWidget>


         <FrameLayout
             android:id="@android:id/tabcontent"
             android:layout_width="0dp"
             android:layout_height="0dp"
             android:layout_weight="0" />

         <android.support.v4.view.ViewPager
             android:id="@+id/viewpager"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             />
     </LinearLayout>
 </TabHost>

代码:

for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
{
    TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
    tv.setTextColor(Color.parseColor(SM.getTheme_colour()));
    tv.setTypeface(font_k);
}

这段代码可以用于更改TabHost文本的颜色、字体(字形)和文字大小。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接