想要在Android中更改小部件的字体大小

时间:2015-05-23 13:41:38

标签: android tabs widget

我的活动中有4个标签,我想更改字体大小,但问题是只有第一个标签更改其字体大小,但其他三个保持不变。 这是代码 plz help

package zubair.example.com.tabproject;

     import android.app.TabActivity;
     import android.content.Intent;
     import android.os.Bundle;
     import android.util.Log;
     import android.view.Menu;
     import android.view.MenuItem;
     import android.view.View;
     import android.widget.LinearLayout;
     import android.widget.RelativeLayout;
     import android.widget.TabHost;
     import android.widget.TabWidget;
     import android.widget.TextView;







    public class Tabsmain extends TabActivity implements TabHost.OnTabChangeListener{


    /** Called when the activity is first created. */
    TabHost tabHost;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_main);


        // Get TabHost Refference
        tabHost = getTabHost();

        // Set TabChangeListener called when tab changed
        tabHost.setOnTabChangedListener(this);
        TabHost.TabSpec spec;
        Intent intent;


        /************* TAB1 ************/
        // Create  Intents to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, Tab1.class);
        spec = tabHost.newTabSpec("First").setIndicator("AboutUs")
                .setContent(intent);

        //Add intent to tab
        tabHost.addTab(spec);


        /************* TAB2 ************/
        intent = new Intent().setClass(this, Tab2.class);
        spec = tabHost.newTabSpec("Second").setIndicator("our Firm")
                .setContent(intent);
        tabHost.addTab(spec);

        /************* TAB3 ************/
        intent = new Intent().setClass(this, Tab3.class);
        spec = tabHost.newTabSpec("Third").setIndicator("Location")
                .setContent(intent);
        tabHost.addTab(spec);


        /************* TAB4 ************/
        // Create  Intents to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, Tab4.class);
        spec = tabHost.newTabSpec("Fourth").setIndicator("ContactUs")
                .setContent(intent);
        tabHost.addTab(spec);

    }

    @Override
    public void onTabChanged(String tabId) {


       TabWidget tw = (TabWidget)tabHost.findViewById(android.R.id.tabs);

        View tabView = tw.getChildTabViewAt(0);
        TextView tv = (TextView)tabView.findViewById(android.R.id.title);
        tv.setTextSize(10);




        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
        {
            if(i==0)
                tabHost.getTabWidget().getChildAt(i);
            else if(i==1)
                tabHost.getTabWidget().getChildAt(i);
            else if(i==2)
                tabHost.getTabWidget().getChildAt(i);
            else if(i==3)
                tabHost.getTabWidget().getChildAt(i);
        }

        Log.i("tabs", "CurrentTab: " + tabHost.getCurrentTab());

        if(tabHost.getCurrentTab()==0)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab());
        else if(tabHost.getCurrentTab()==1)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab());
        else if(tabHost.getCurrentTab()==2)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab());
        else if(tabHost.getCurrentTab()==3)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab());

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_tab_bar, menu);
        return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
        }

       }

1 个答案:

答案 0 :(得分:0)

您只需为第一个标签设置文字大小。迭代所有选项卡并设置文本大小,如此

View tabView;
TextView tv;
for(int i = 0; i < tw.getTabCount(); i++){
    tabView = tw.getChildTabViewAt(i);
    tv = (TextView)tabView.findViewById(android.R.id.title);
    tv.setTextSize(10);
}

编辑:

此外,在onCreate方法而不是onTabChanged方法中可能会更好,因为您只需要设置一次文本大小。

最后,我不确定这段代码是什么意思,但我认为它实际上没有完成任何事情:

for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
    if(i==0)
        tabHost.getTabWidget().getChildAt(i);
    else if(i==1)
        tabHost.getTabWidget().getChildAt(i);
    else if(i==2)
        tabHost.getTabWidget().getChildAt(i);
    else if(i==3)
        tabHost.getTabWidget().getChildAt(i);
}

Log.i("tabs", "CurrentTab: " + tabHost.getCurrentTab());

if(tabHost.getCurrentTab()==0)
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab());
else if(tabHost.getCurrentTab()==1)
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab());
else if(tabHost.getCurrentTab()==2)
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab());
else if(tabHost.getCurrentTab()==3)
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab());