自动生成TabHost,TabHost背景颜色设置
来源:互联网 发布:js 颜色 编辑:程序博客网 时间:2024/05/16 05:52
1.布局、配置文件
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dsp.tvshow" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" android:icon="@drawable/icon" android:label="@string/app_name" android:logo="@drawable/logo"> <activity android:name="com.dsp.activity.MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
res/layout/layout_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/logoAreaLayout" android:layout_width="fill_parent" android:layout_height="50dp" android:gravity="left"> <ImageView android:id="@+id/logImageView" android:layout_width="wrap_content" android:layout_height="fill_parent" android:src="@drawable/sitv_logo" android:paddingRight="30dp" android:layout_gravity="center"/> <TextView android:id="@+id/logoTextView" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="@string/logo_text" android:textSize="30dp" android:gravity="center"/> </LinearLayout> <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:id="@+id/tabLinearLayout" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="0" android:gravity="center"> </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:id="@+id/videoInfoArea" android:layout_width="fill_parent" android:layout_height="50dp" > <TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="center" android:layout_marginLeft="200dp" android:layout_alignParentLeft="true" android:text="一共有1000个视频" android:textSize="30dp"/> <TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="center" android:layout_marginRight="400dp" android:layout_alignParentRight="true" android:layout_marginLeft="60dp" android:text="1/100页" android:textSize="30dp"/> <Button android:id="@+id/forwardBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/label"android:layout_marginLeft="30dp" android:text="@string/forwardBtnText" android:textSize="20dp"/> <Button android:id="@+id/backwardBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/forwardBtn" android:text="@string/backwardBtnText" android:textSize="20dp"/> </RelativeLayout> <GridView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/videoGridView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="90dp" android:numColumns="5"android:verticalSpacing="10dp"android:horizontalSpacing="10dp"android:stretchMode="columnWidth"android:gravity="center"/> </FrameLayout> </LinearLayout></TabHost></LinearLayout>
<resources> <string name="app_name">TVShow</string> <string name="logo_text">ACR创新应用示范-互联网视频聚合</string> <string name="backwardBtnText">上一页</string> <string name="forwardBtnText">下一页</string></resources>
res/values/styles.xml:
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Base application theme for API 11+. This theme completely replaces AppBaseTheme from res/values/styles.xml on API 11+ devices. --><style name="AppTheme" parent="android:Theme.Holo.Light"> <!-- API 11 theme customizations can go here. --></style></resources>
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@android:drawable/screen_background_light" /> <item android:state_active="true" android:drawable="@android:drawable/alert_dark_frame" /> <item android:drawable="@android:drawable/screen_background_dark" /></selector>
2.Java代码
MainActivity.java
import java.util.List;import com.dsp.entity.gson.ProgramInfo;import com.dsp.tvshow.R;import com.dsp.util.ProgramInfoHelper;import android.app.TabActivity;import android.os.Bundle;import android.view.View;import android.widget.TabHost;import android.widget.TabHost.OnTabChangeListener;import android.widget.TabWidget;import android.widget.TextView;public class MainActivity extends TabActivity implements OnTabChangeListener{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.layout_main);//获取栏目列表List<ProgramInfo> programs = new ProgramInfoHelper().getProgramList();TabHost tabHost = getTabHost();TabWidget tabWidget = tabHost.getTabWidget();for(ProgramInfo p : programs){TabHost.TabSpec tabSpec;//实例化一个分页tabSpec = tabHost.newTabSpec(p.getProgramId());//View tab = createTabIndicatorView(tabWidget, p.getProgramName(), getResources().getDrawable(R.drawable.tab_bg));//tabSpec.setIndicator(tab);//设置分页的标题tabSpec.setIndicator(p.getProgramName());//设置分页的内容tabSpec.setContent(android.R.id.tabcontent);tabHost.addTab(tabSpec);}for(int i = 0; i < tabWidget.getChildCount(); i++){//获取tabview项 View view = tabWidget.getChildTabViewAt(i);//设置tab背景颜色,对应配置文件的tab_bg.xml,可变化的背景,选中时为白色,未选中为黑色view.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg));//获取textview控件TextView textView = (TextView)view.findViewById(android.R.id.title);textView.setGravity(BIND_AUTO_CREATE);//自动变换颜色,但是没实现//textView.setTextColor(getResources().getColor(R.color.tabtext_color));if(i == 0){//默认的初始页为第一页,tab的文字颜色设为白色textView.setTextColor(getResources().getColor(android.R.color.black));}else{//未选中的页的tab的文字设为黑色textView.setTextColor(getResources().getColor(android.R.color.white));}//设置tab的文字大小textView.setTextSize(30);}tabHost.setCurrentTab(0);//tabchanged的监听tabHost.setOnTabChangedListener(this);}//改变tab时的处理@Override/* * @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String) * @param tabId 新建TabSpec时设置的id */public void onTabChanged(String tabId) {TabHost tabHost = getTabHost();int curTabID = getTabHost().getCurrentTab();TabWidget tabWidget = tabHost.getTabWidget();View view = tabWidget.getChildTabViewAt(curTabID);TextView textView = (TextView)view.findViewById(android.R.id.title);//将选中页tab的文字设为黑色textView.setTextColor(getResources().getColor(android.R.color.black));for(int i = 0; i < tabWidget.getChildCount(); i++){if(i != curTabID){//未选中页tab的文字设为白色((TextView)tabWidget.getChildTabViewAt(i).findViewById(android.R.id.title)).setTextColor(getResources().getColor(android.R.color.white));}}}/* * 生成标准TAB标头的工具 * http://www.eoeandroid.com/thread-51167-1-1.html * @param parent The parent ViewGroup to attach the new view to. * @param label The label to display in the tab indicator. If null, not label will be displayed. * @param icon The icon to display. If null, no icon will be displayed. * @return The tab indicator View. *//*private View createTabIndicatorView(ViewGroup parent, CharSequence label, Drawable icon) { final LayoutInflater inflater = LayoutInflater.from(this); final View tabIndicator = inflater.inflate(R.layout.tab_indicator, parent, false); final TextView tv = (TextView) tabIndicator.findViewById(R.id.tab_title); tv.setText(label); final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.tab_icon); iconView.setImageDrawable(icon); return tabIndicator; } */}
3.效果
参考:
TabSpec和TabHost实例
android TabHost解决下面白线
如何设置TabHost标签里面的文字的颜色
[Android实例] 史上最全的Android的Tab与TabHost讲解
- 自动生成TabHost,TabHost背景颜色设置
- 自动生成TabHost,TabHost背景颜色设置
- tabhost如何设置背景
- tabhost背景
- TabHost
- tabhost
- Tabhost
- TabHost
- tabHost
- TabHost
- TabHost
- tabhost
- Tabhost
- TabHost
- TabHost
- TabHost
- TabHost
- TabHost
- Java设计模式十八:代理模式(Proxy)
- poj 2923 Relocation(DP+状态压缩+01背包)
- Java IO之RandomAccessFile类
- 类的访问控制
- GDAL读取S-57海图数据中文属性值乱码问题解决
- 自动生成TabHost,TabHost背景颜色设置
- 第八周任务
- HDOJ1711 Number Sequence
- 图片上传类
- 几个概念:javascript闭包及柯里化
- Linux 查看系统硬件信息(实例详解)
- Java设计模式十七:外观模式(Facade)
- DAPM之四:dapm widget events
- 《c和指针》笔记--自由的c代码