/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.test.widget;
import java.util.ArrayList;
import java.util.List;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.test.activity.R;
/**
* Container for a tabbed window view. This object holds two children: a set of tab labels that the
* user clicks to select a specific tab, and a FrameLayout object that displays the contents of that
* page. The individual elements are typically controlled using this container object, rather than
* setting values on the child elements themselves.
*/
public class MyTabHost extends FrameLayout implements ViewTreeObserver.OnTouchModeChangeListener {
private MyTabWidget mTabWidget;
private FrameLayout mTabContent;
private List<TabSpec> mTabSpecs = new ArrayList<TabSpec>(2);
/**
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
protected int mCurrentTab = -1;
private View mCurrentView = null;
/**
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
protected LocalActivityManager mLocalActivityManager = null;
private OnTabChangeListener mOnTabChangeListener;
private OnKeyListener mTabKeyListener;
public MyTabHost(Context context) {
super(context);
initTabHost();
}
public MyTabHost(Context context, AttributeSet attrs) {
super(context, attrs);
initTabHost();
}
private void initTabHost() {
setFocusableInTouchMode(true);
setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
mCurrentTab = -1;
mCurrentView = null;
}
/**
* Get a new {@link TabSpec} associated with this tab host.
* @param tag required tag of tab.
*/
public TabSpec newTabSpec(String tag) {
return new TabSpec(tag);
}
/**
* <p>Call setup() before adding tabs if loading TabHost using findViewById().
* <i><b>However</i></b>: You do not need to call setup() after getTabHost()
* in {@link android.app.TabActivity TabActivity}.
* Example:</p>
<pre>mTabHost = (TabHost)findViewById(R.id.tabhost);
mTabHost.setup();
mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1");
*/
public void setup() {
mTabWidget = (MyTabWidget) findViewById(R.id.tabs);
if (mTabWidget == null) {
throw new RuntimeException(
"Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");
}
// KeyListener to attach to all tabs. Detects non-navigation keys
// and relays them to the tab content.
mTabKeyListener = new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_ENTER:
return false;
}
mTabContent.requestFocus(View.FOCUS_FORWARD);
return mTabContent.dispatchKeyEvent(event);
}
};
mTabWidget.setTabSelectionListener(new MyTabWidget.OnTabSelectionChanged() {
public void onTabSelectionChanged(int tabIndex, boolean clicked) {
setCurrentTab(tabIndex);
if (clicked) {
mTabContent.requestFocus(View.FOCUS_FORWARD);
}
}
});
mTabContent = (FrameLayout) findViewById(R.id.tabcontent);
if (mTabContent == null) {
throw new RuntimeException(
"Your TabHost must have a FrameLayout whose id attribute is "
+ "'android.R.id.tabcontent'");
}
}
/**
* If you are using {@link TabSpec#setContent(android.content.Intent)}, this
* must be called since the activityGroup is needed to launch the local activity.
*
* This is done for you if you extend {@link android.app.TabActivity}.
* @param activityGroup Used to launch activities for tab content.
*/
public void setup(LocalActivityManager activityGroup) {
setup();
mLocalActivityManager = activityGroup;
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
final ViewTreeObserver treeObserver = getViewTreeObserver();
if (treeObserver != null) {
treeObserver.addOnTouchModeChangeListener(this);
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
final ViewTreeObserver treeObserver = getViewTreeObserver();
if (treeObserver != null) {
treeObserver.removeOnTouchModeChangeListener(this);
}
}
/**
* {@inheritDoc}
*/
public void onTouchModeChanged(boolean isInTouchMode) {
if (!isInTouchMode) {
// leaving touch mode.. if nothing has focus, let's give it to
// the indicator of the current tab
if (mCurrentView != null && (!mCurrentView.hasFocus() || mCurrentView.isFocused())) {
mTabWidget.getChildTabViewAt(mCurrentTab).requestFocus();
}
}
}
/**
* Add a tab.
* @param tabSpec Specifies how to create the indicator and content.
*/
public void addTab(TabSpec tabSpec) {
if (tabSpec.mIndicatorStrategy == null) {
throw new IllegalArgumentException("you must specify a way to create the tab indicator.");
}
if (tabSpec.mContentStrategy == null) {
throw new IllegalArgumentException("you must specify a way to create the tab content");
}
View tabIndicator = tabSpec.mIndicatorStrategy.createIndicatorView();
tabIndicator.setOnKeyListener(mTabKeyListener);
// If this is a custom view, then do not draw the bottom strips for
// the tab indicators.
if (tabSpec.mIndicatorStrategy instanceof ViewIndicatorStrategy) {
mTabWidget.setDrawBottomStrips(false);
}
mTabWidget.addView(tabIndicator);
mTabSpecs.add(tabSpec);
if (mCurrentTab == -1) {
setCurrentTab(0);
}
}
/**
* Removes all tabs from the tab widget associated with this tab host.
*/
public void clearAllTabs() {
mTabWidget.removeAllViews();
initTabHost();
mTabContent.removeAllViews();
mTabSpecs.clear();
requestL
没有合适的资源?快使用搜索试试~ 我知道了~
Android TabHost 标签在屏幕左侧样例
共93个文件
png:40个
class:27个
xml:12个
5星 · 超过95%的资源 需积分: 21 288 下载量 48 浏览量
2012-12-26
17:24:06
上传
评论 3
收藏 165KB RAR 举报
温馨提示
由于android TabHost标签默认只能显示在屏幕上、下两侧,该样例为修改源码,自定义TabHost,实现标签栏居于屏幕左侧或右侧(居右侧,则只需要修改标签控件的相对布居位置)。
资源推荐
资源详情
资源评论
收起资源包目录
VerticalTabHost.rar (93个子文件)
VerticalTabHost
AndroidManifest.xml 874B
res
layout
test_activity2.xml 468B
test_activity1.xml 459B
tab_indicator.xml 1KB
main.xml 2KB
drawable-ldpi
ic_launcher.png 2KB
drawable
tab_bottom_left_v4.xml 1KB
tab_indicator_v4.xml 1KB
tab_bottom_left.xml 1KB
tab_bottom_right_v4.xml 1KB
tab_bottom_right.xml 1KB
tab_indicator.xml 1KB
setting.png 2KB
group.png 598B
contact.png 2KB
drawable-mdpi
ic_launcher.png 3KB
drawable-hdpi
tab_press_bar_right.9.png 146B
tab_selected_bar_right.9.png 150B
tab_selected_bar_left_v4.9.png 151B
tab_selected_bar_right_v4.9.png 151B
tab_press_bar_left.9.png 146B
tab_selected.9.png 953B
tab_focus_bar_left.9.png 143B
tab_unselected_v4.9.png 975B
tab_unselected.9.png 1KB
tab_press.9.png 304B
tab_selected_bar_left.9.png 150B
tab_focus.9.png 304B
tab_focus_bar_right.9.png 143B
ic_launcher.png 4KB
tab_selected_v4.9.png 998B
values
strings.xml 196B
proguard.cfg 1KB
src
com
test
widget
MyTabHost.java 24KB
MyTabWidget.java 15KB
activity
MainActivity.java 975B
TestActivity1.java 297B
TestActivity2.java 295B
bin
classes
com
test
widget
MyTabHost$1.class 1KB
MyTabWidget$TabClickListener.class 1KB
MyTabHost$LabelAndIconIndicatorStrategy.class 2KB
MyTabHost$LabelAndIconIndicatorStrategyFromRes.class 2KB
MyTabHost$TabSpec.class 4KB
MyTabHost$2.class 989B
MyTabHost$IntentContentStrategy.class 2KB
MyTabHost$IndicatorStrategy.class 269B
MyTabHost$ViewIdContentStrategy.class 2KB
MyTabHost.class 9KB
MyTabWidget.class 6KB
MyTabHost$ContentStrategy.class 286B
MyTabWidget$OnTabSelectionChanged.class 269B
MyTabHost$ViewIndicatorStrategy.class 956B
MyTabHost$TabContentFactory.class 284B
MyTabHost$FactoryContentStrategy.class 1KB
MyTabHost$LabelIndicatorStrategy.class 2KB
MyTabHost$OnTabChangeListener.class 266B
activity
R.class 524B
MainActivity.class 2KB
R$layout.class 504B
R$attr.class 337B
TestActivity1.class 518B
R$id.class 588B
TestActivity2.class 518B
R$string.class 424B
R$drawable.class 1KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable
setting.png 1KB
group.png 390B
contact.png 1KB
drawable-mdpi
ic_launcher.png 2KB
drawable-hdpi
tab_press_bar_right.9.png 158B
tab_selected_bar_right.9.png 149B
tab_selected_bar_left_v4.9.png 149B
tab_selected_bar_right_v4.9.png 149B
tab_press_bar_left.9.png 158B
tab_selected.9.png 305B
tab_focus_bar_left.9.png 158B
tab_unselected_v4.9.png 334B
tab_unselected.9.png 339B
tab_press.9.png 302B
tab_selected_bar_left.9.png 149B
tab_focus.9.png 307B
tab_focus_bar_right.9.png 158B
ic_launcher.png 4KB
tab_selected_v4.9.png 347B
TabActivityTest.apk 39KB
classes.dex 21KB
VerticalTabHost.apk 39KB
resources.ap_ 26KB
.classpath 364B
assets
project.properties 360B
.project 851B
gen
com
test
activity
R.java 3KB
共 93 条
- 1
zhangyi1288
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页