如何更改TabHost标题?

时间:2015-09-02 05:39:58

标签: android android-tabhost

我尝试将TabHost的Tabs文本更改为文本,使其显示在singleLine=true上。要做到这一点,我尝试创建TextView并添加此属性singleLine。问题是我不能在TabHost中插入这个TextView。

我怎么能这样做?

我正在尝试这个。

XML TabHost

<android.support.v4.app.FragmentTabHost
    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="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_weight="0"
            android:layout_gravity="bottom">
        </TabWidget>

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

</android.support.v4.app.FragmentTabHost>

的TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:id="@+id/perfil"
        android:text="teste de aba para tab host"
        />

</LinearLayout>

活动

public class AreaAlunoMainActivity extends FragmentActivity {
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.areadoaluno_main);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

        mTabHost.addTab(
                mTabHost.newTabSpec("perfil").setIndicator("Perfil", null).setContent(R.id.perfil),
                PerfilFrag.class, null);

    }
}

1 个答案:

答案 0 :(得分:1)

    TabHost.TabSpec spec = mTabHost.newTabSpec(TAB_A);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(android.R.id.tabcontent);
        }
    });
     spec.setIndicator(createTabView(TAB_A, R.drawable.salonsphp));
    mTabHost.addTab(spec);


    private View createTabView(final String text, final int id) {
         View view =LayoutInflater.from(this).inflate(R.layout.yourcustomlayout,null);
        ImageView imageView = (ImageView) view.findViewById(R.id.tab_icon);
        imageView.setImageDrawable(getResources().getDrawable(id));
        ((TextView) view.findViewById(R.id.tab_text)).setText(text);
        return view;
    }

you can set specifi. of tab like this.....