android 学习笔记6——分页标签之TabHost
16lz
2021-01-24
分页标签的实现,包括切换事件的监听、tab 的 颜色 背景 字体 的改变、全屏的设置。
.java代码:
package com.example.change;import android.R.color;import android.app.Activity;import android.app.TabActivity;import android.graphics.Color;import android.graphics.Typeface;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.TabHost;import android.widget.TabHost.OnTabChangeListener;import android.widget.TextView;public class MainActivity extends TabActivity { private TabHost tabHost; private void addTab() { tabHost.addTab(tabHost .newTabSpec("tab1") .setIndicator("TAB1", getResources().getDrawable(R.drawable.face)) .setContent(R.id.textview1)); tabHost.addTab(tabHost .newTabSpec("tab2") .setIndicator("TAB2") .setContent(R.id.textview2)); tabHost.addTab(tabHost .newTabSpec("tab3") .setIndicator("TAB3", getResources().getDrawable(R.drawable.ic_launcher)) .setContent(R.id.textview3)); } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tabHost = getTabHost(); addTab(); tabHost.setCurrentTab(0); tabHost.setOnTabChangedListener(new OnTabChangeListener(){ public void onTabChanged(String tabId){ updateTab(tabHost); } }); //updateTab(tabHost); } private void updateTab(final TabHost tabHost) { for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) { View view = tabHost.getTabWidget().getChildAt(i); TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); tv.setTextSize(16); tv.setTypeface(Typeface.SERIF, 2); if (tabHost.getCurrentTab() == i) { view.setBackgroundColor(Color.BLUE); tv.setTextColor(Color.WHITE); } else { view.setBackgroundDrawable(getResources().getDrawable(R.drawable.face2)); tv.setTextColor(Color.BLACK); } } } }
manifest 代码:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.change" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
xml代码:
<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"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/textview1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Linux" android:textColor="#FF0000"/> <TextView android:id="@+id/textview2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="MAC" android:textColor="#385E0F" /> <TextViewandroid:id="@+id/textview3"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="Windows"android:textColor="#1E90FF" /> </FrameLayout> </LinearLayout></TabHost>
更多相关文章
- Android(安卓)自定义AlertDialog 去黑边终极解决方案(亲测有效!)
- android java js 回调 真心好用
- android 获取手机图片裁剪后分享 2
- Android触摸事件小小画板(7)
- android 动态设置margin
- Android(安卓)代码规范 code style
- Android(安卓)显示手机电池的当前电量
- Android(安卓)代码提示无效