位置: 首页>> Android TabHost中标签Tab的高度和宽度

Android TabHost中标签Tab的高度和宽度

设置Tab高度和宽度

final TabWidget tabWidget = tabHost.getTabWidget();

for (int i =0; i < tabWidget.getChildCount(); i++) {

tabWidget.getChildAt(i).getLayoutParams().height = 60;

tabWidget.getChildAt(i).getLayoutParams().width = 65;

}

注意:若要使设置的高度和宽度有效,在界面定义文件中TabWidget的布局参数的宽度和高度应设置为wrap_content,如

<TabWidget android:id=”@android:id/tabs” android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
例子:改变我的下载、我评过分宽度和高度并居中显示
QQ图片20130625185921
代码如下:
GcMyScoreActivity.Java文件

package com.jwd.gameclub.activity;

import com.jwd.gameclub.R;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
/**
* @date 2013.6.25
* @author zfl
* 我的评分包含我的下载、我评过分
*/
public class GcMyScoreActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//去标题
setContentView(R.layout.myscore);

TabHost mTabHost = getTabHost();
TabWidget tabWidget = mTabHost.getTabWidget();
mTabHost.addTab(mTabHost.newTabSpec(“download”).setIndicator(
“我的下载”).setContent(
new Intent(this, GcMyScoreDownloadActivity.class)));
mTabHost.addTab(mTabHost.newTabSpec(“review”).setIndicator(
“我评过分”).setContent(
new Intent(this, GcMyScoreReviewActivity.class)));
mTabHost.setCurrentTab(0);

for (int i =0; i < tabWidget.getChildCount(); i++) {

/**设置高度、宽度*/
tabWidget.getChildAt(i).getLayoutParams().height = 45;
tabWidget.getChildAt(i).getLayoutParams().width = 150;

/**设置tab中标题文字的颜色,不然默认为黑色 */
final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));
}
}
}
MyScore.XML文件

 

<?xml version=”1.0″ encoding=”utf-8″?>
<TabHost xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@android:id/tabhost”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”@drawable/bg”>
<RelativeLayout
android:id=”@+id/title”
android:layout_width=”fill_parent”
android:layout_height=”50dip”
android:background=”@drawable/bg_navigation” >
<View android:background=”@drawable/sub”
android:layout_width=”50.0dip”
android:layout_height=”50.0dip”
android:layout_marginLeft=”5dip”
android:gravity=”center”
/>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:layout_centerVertical=”true”
android:layout_marginLeft=”5dip”
android:gravity=”center”
android:text=”我的评分”
android:textSize=”25dip” />
</RelativeLayout>

<android.support.v4.view.ViewPager
android:id=”@+id/vPager”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:layout_gravity=”center”
android:paddingBottom=”55dip”
android:persistentDrawingCache=”animation” />

<LinearLayout android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:layout_marginTop=”55dip”
>
<TabWidget android:id=”@android:id/tabs”
android:layout_alignParentBottom=”true”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center”/>

<FrameLayout android:id=”@android:id/tabcontent”
android:layout_weight=”1″
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >
</FrameLayout>
</LinearLayout>

</TabHost>

红色代码需要注意,如果不这样写,宽度改不了,居中也改不了。

No Responses To This Post So Far(Rss)

Post a comment