更改android中选项卡中的文本大小

时间:2013-10-18 05:15:41

标签: android android-tabs

我一直在标签栏中遇到文字大小的问题。下面是图片,

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;

结果图像

enter image description here

但我希望输出为此图像

enter image description here

如上图所示,我的标签小部件应该是可移动的,我该怎么做?

8 个答案:

答案 0 :(得分:4)

TextView x = (TextView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
    x.setTextSize(25);

请参阅以下链接以获取更多详细信息

Change text size on tabHost

How to change the font size of tabhost in android

答案 1 :(得分:3)

您可以使用TabLayout和Viewpager,并将tabMode设置为可滚动,请参阅以下代码:

    <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" />

答案 2 :(得分:1)

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

在选项卡xml中使用此行,它将执行此操作。

答案 3 :(得分:0)

您还可以自定义标签的文字和背景。请参阅此博客,了解如何做到这一点tutorial

答案 4 :(得分:0)

简单方法只需在ur 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>

别忘了在清单文件中添加带样式名称的主题。

答案 5 :(得分: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 =&#34;可滚动&#34;

答案 6 :(得分:0)

将TabLayout放在Horizo​​ntalScrollView中:

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{

    if (event->type() == QEvent::KeyPress){
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
        if (keyEvent->key() == Qt::Key_Escape){
            //your code here
            emit sigHalted();
            return true;
        }
        else{
            return false;
        }
    }
    else
        return QMainWindow::eventFilter(watched,event);
}

答案 7 :(得分:0)

<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更改文本颜色,类型面(字体样式)以及文本大小。