我按照android开发tutorial标签布局来实现简单的标签布局。
基于该教程,我脑子里有一个问题,那就是如何更改标签字体大小?
我尝试通过在布局 xml 文件的android:textSize="8dip"
中添加属性<TabWidget ...>
来更改标签字体大小:
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="8dip"
/>
但它没有任何效果。
任何人都可以提供更改标签上字体大小的正确方法吗?
答案 0 :(得分:11)
您可以在此处使用
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);
现在,您可以更改文字的颜色,尺寸和字体
答案 1 :(得分:5)
如果你想实现它,你应该扩大标签的布局。
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>
您应该根据您的情况将testview放在图像视图和settest文本大小属性的位置。 我希望这是有帮助的。
答案 2 :(得分:1)
这是我的2美分,它对我有用......
首先在你的oncreate中添加
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
MyView view = null;
然后在你的on创建中使用this for each tab ...
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方法中更改文本和背景
答案 3 :(得分:0)
我不是你可以改变TabWidget
中的textSize
我只是检查您可以使用的属性,并且无法直接编辑您的文本。
唯一的方法是给自己的布局充气。