【问题标题】:Newbie: How to change tab font size?新手:如何更改标签字体大小?
【发布时间】:2011-09-25 19:21:15
【问题描述】:

我跟着android开发的tutorial的tab layout来实现一个简单的tab布局。

根据那个教程,我想到了一个问题,那就是如何更改标签字体大小??

我尝试通过在布局 xml 文件的<TabWidget ...> 中添加属性android:textSize="8dip" 来更改标签字体大小:

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

    android:textSize="8dip" 
/>

但它没有任何效果。

任何人都可以提供正确的方法来更改选项卡上的字体大小吗?

【问题讨论】:

    标签: android android-layout android-emulator


    【解决方案1】:

    如果你想实现它,你应该增加标签的布局。

        tabHost = getTabHost(); // The activity TabHost
        tabHost.setOnTabChangedListener(this);
        Resources res = getResources(); // Resource object to get Drawables
        tabHost = getTabHost(); // The activity TabHost
        TabHost.TabSpec spec; // Reusable TabSpec for each tab
    
        TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
        TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
        TabSpec thirdTabSpec = tabHost.newTabSpec("tid3");
        TabSpec fourthTabSpec = tabHost.newTabSpec("tid4");
        TabSpec fifthTabSpec = tabHost.newTabSpec("tid5");
    
        viewCache[0] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
        viewCache[1] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
        viewCache[2] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
        viewCache[3] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
        viewCache[4] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
    
        firstTabSpec.setIndicator(viewCache[0]);
        secondTabSpec.setIndicator(viewCache[1]);
        thirdTabSpec.setIndicator(viewCache[2]);
        fourthTabSpec.setIndicator(viewCache[3]);
        fifthTabSpec.setIndicator(viewCache[4]);
    
        firstTabSpec.setContent(new Intent(this, HomeTabActivityGroup.class));
        secondTabSpec
                .setContent(new Intent(this, ProfileTabActivityGroup.class));
        thirdTabSpec.setContent(new Intent(this,
                NotificationTabActivityGroup.class));
        fourthTabSpec.setContent(new Intent(this,
                FavoritesTabActivityGroup.class));
        fifthTabSpec
                .setContent(new Intent(this, MoreTabActivityGroupNew.class));
    
        tabHost.addTab(firstTabSpec);
        tabHost.addTab(secondTabSpec);
        tabHost.addTab(thirdTabSpec);
        tabHost.addTab(fourthTabSpec);
        tabHost.addTab(fifthTabSpec);
    
        currentTabvalue = tabHost.getCurrentTab();
        C2DMessaging.register(TennisAppActivity.mContext,
                "racquetester@gmail.com");
        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
    
            // tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#7392B5"));
            switch (i) {
            case 0:
                tabHost.getTabWidget().getChildAt(i)
                        .setBackgroundResource(R.drawable.home);
                break;
            case 1:
                tabHost.getTabWidget().getChildAt(i)
                        .setBackgroundResource(R.drawable.profile);
                break;
            case 2:
                tabHost.getTabWidget().getChildAt(i)
                        .setBackgroundResource(R.drawable.notifications);
                break;
            case 3:
                tabHost.getTabWidget().getChildAt(i)
                        .setBackgroundResource(R.drawable.fav);
                break;
            case 4:
                tabHost.getTabWidget().getChildAt(i)
                        .setBackgroundResource(R.drawable.more);
                break;
            }
        }
    

    //**************************************

    这是tab1.xml

    <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout android:id="@+id/LinearLayout01" 
     android:layout_width="wrap_content" android:layout_height="wrap_content"
      xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center">
     <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content"  android:layout_height="50dip"></ImageView>
       </LinearLayout>
    

    您应该根据您的需要在图像视图的位置放置一个测试视图并设置测试文本大小属性。 我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我不确定你可以在 TabWidget 中更改 textSize 我只是检查您可以使用的属性,无法直接编辑您的文本。

      唯一的方法是增加你自己的布局。

      【讨论】:

        【解决方案3】:

        你的 tabactivity 使用在这里

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = (TabHost) getTabHost(); // The activity TabHost
        
        TabHost.TabSpec spec; // Resusable TabSpec for each tab
        Typeface localTypeface1 = Typeface.createFromAsset(getAssets(),
                "fonts/arial.ttf");
        

        编辑开始

        tabHost.getTabWidget().setBackgroundDrawable( getResources().getDrawable(R.drawable.bluenavbar));
        

        编辑结束

        TextView txtTab = new TextView(this);
        txtTab.setText(getString(R.string.top_news));
        txtTab.setPadding(8, 9, 8, 9);
        txtTab.setTextColor(Color.WHITE);
        txtTab.setTextSize(14);
        txtTab.setTypeface(localTypeface1);
        txtTab.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        txtTab.setBackgroundResource(R.drawable.tab_news);
        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("topNews").setIndicator(txtTab).setContent(new Intent(this, TopNewsGroup.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        tabHost.addTab(spec);
        

        现在您可以更改文本的颜色大小字体

        【讨论】:

        • 但是这个实现禁用了选项卡被选中时的选项卡背景颜色变化
        • @Leem:在我的情况下,我使用图像作为标签背景,请参阅编辑
        • 需要注意的是,TabActivity 在 API 13 中已被弃用。
        【解决方案4】:

        这是我的 2 美分,它对我有用...

        首先在你的 oncreate 中添加这个

        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
            Intent intent;  // Reusable Intent for each tab
            MyView view = null;
        

        然后在你的 on create 中为每个标签使用这个...

        intent = new Intent().setClass(this, BrownieTab.class);
            view = new MyView(this, R.drawable.ic_tab_brownie_2, R.drawable.ic_tab_brownie_2, "Yourself");
            view.setBackgroundDrawable(this.getResources().getDrawable(R.layout.selecttabbackground));
            spec = tabHost.newTabSpec("inprogress").setIndicator(view).setContent(intent);
            tabHost.addTab(spec);
        

        最后是 MyView...

        private class MyView extends LinearLayout {
            ImageView iv;
            TextView tv;
        
            public MyView(Context c, int drawable, int drawableselec, String label) {
                super(c);
                iv = new ImageView(c);
                StateListDrawable listDrawable = new StateListDrawable();
                listDrawable.addState(SELECTED_STATE_SET, this.getResources()
                        .getDrawable(drawableselec));
                listDrawable.addState(ENABLED_STATE_SET, this.getResources()
                        .getDrawable(drawable));
                iv.setImageDrawable(listDrawable);
                iv.setBackgroundColor(Color.TRANSPARENT);
                iv.setLayoutParams(new LayoutParams(48, 48, (float) 0.0));
                setGravity(Gravity.CENTER);
                tv = new TextView(c);
                tv.setText(label);
                tv.setGravity(Gravity.CENTER);
                tv.setBackgroundColor(Color.TRANSPARENT);
                tv.setTextColor(Color.BLACK);
                tv.setTextSize(12);
        
                tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT, (float) 1.0));
                setOrientation(LinearLayout.VERTICAL);
                addView(iv);
                addView(tv);
                setBackgroundDrawable(this.getResources().getDrawable(
                        R.layout.selecttabbackground));
            }
        }   
        

        您可以在我的视图中更改有关选项卡的所有属性,并在 oncreate 方法中更改文本和背景

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-05
          • 2022-09-28
          相关资源
          最近更新 更多