如何在我的选项卡小部件中设置文本

时间:2016-05-16 10:13:10

标签: android tabwidget

我想更改标签栏中的文字以获取singleLine中的文字并删除粗体样式但该怎么做?请告诉我好方法 我的下面代码没有改变任何内容......

这是我的XMl代码:

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

    <TabHost
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabHost"
        android:layout_gravity="right">

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

                <LinearLayout
                    android:id="@+id/linearLayout1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="5dp"
                        android:id="@+id/text1"
                        android:text="@string/text_v1"/>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/linearLayout2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="5dp"
                        android:id="@+id/text2"
                        android:text="@string/text_v2"/>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/linearLayout3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="5dp"
                        android:id="@+id/text3"
                        android:text="@string/text_v3"/>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/linearLayout4"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="5dp"
                        android:id="@+id/text4"
                        android:text="@string/text_v4"/>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/linearLayout5"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="5dp"
                        android:id="@+id/text5"
                        android:text="@string/text_v5"/>
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

这里是java代码:

package com.atonfoaziz.tabs5;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TabHost tabHost;
    TabHost.TabSpec spec;

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

        tabHost = (TabHost)findViewById(R.id.tabHost);
        if (tabHost != null){
            tabHost.setup();

            spec = tabHost.newTabSpec("Tab I").setContent(R.id.linearLayout1).setIndicator("Contacts");
            tabHost.addTab(spec);
            spec = tabHost.newTabSpec("Tab II").setContent(R.id.linearLayout2).setIndicator("Calls");
            tabHost.addTab(spec);
            spec = tabHost.newTabSpec("Tab III").setContent(R.id.linearLayout3).setIndicator("Discussions");
            tabHost.addTab(spec);
            spec = tabHost.newTabSpec("Tab IV").setContent(R.id.linearLayout4).setIndicator("Research");
            tabHost.addTab(spec);
            spec = tabHost.newTabSpec("Tab V").setContent(R.id.linearLayout5).setIndicator("Settings");
            tabHost.addTab(spec);

            TabWidget widget = tabHost.getTabWidget();
            int tabCount = widget.getTabCount();
            for (int i = 0; i < tabCount; i++){

                View view = widget.getChildTabViewAt(i);
                if (view != null){

                    TextView textView = (TextView)findViewById(R.id.title);
                    if (textView != null){

                        textView.setAllCaps(false);
                        textView.setSingleLine(true);
                        textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

                        if (Build.VERSION.SDK_INT < 23){
                            textView.setTextAppearance(this, R.style.tab_appereance);
                        } else {
                            textView.setTextAppearance(R.style.tab_appereance);
                        }

                        textView.setTextSize(16);
                    }
                }
            }
        }
    }

}

0 个答案:

没有答案