最近很多小伙伴都在问android–如何在TabHost中获取Tab的内容?和android获取topactivity这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展android
最近很多小伙伴都在问android – 如何在TabHost中获取Tab的内容?和android获取topactivity这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展android fragmenttabhost 如何让tabs位于底部(个人笔记)、android Google Map不会在TabHost中加载、android tabhost、Android TabHost Auto在TabChange的侧面ScrollView中垂直滚动?等相关知识,下面开始了哦!
本文目录一览:- android – 如何在TabHost中获取Tab的内容?(android获取topactivity)
- android fragmenttabhost 如何让tabs位于底部(个人笔记)
- android Google Map不会在TabHost中加载
- android tabhost
- Android TabHost Auto在TabChange的侧面ScrollView中垂直滚动?
android – 如何在TabHost中获取Tab的内容?(android获取topactivity)
<LinearLayout android:id="@+id/TabContainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="99" android:orientation="vertical"> <TabHost android:id="@+android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/TabLinearLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@+android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content"></TabWidget> <FrameLayout android:id="@+android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp"></FrameLayout> </LinearLayout> </TabHost> </LinearLayout>
基本上它是一个带有TextViews作为内容的TabHost.我正在使用createTabContent()动态构建这些TextViews的选项卡和内容.这很好.但在某些情况下(某些事件由带有TCP套接字的AsyncTask处理),我需要更改其中一些TextView的内容.
我确信我的问题是我对Android SDK缺乏经验的结果,但我找不到迭代TabHost并获取每个TextView的内容的方法.
我所取得的“最接近”的方法是:
TabHost th = (TabHost) findViewById(android.R.id.tabhost); TabWidget tw = th.getTabWidget(); for (int i = 0; i < tw.getChildCount(); i++) { LinearLayout lLayout = (LinearLayout) tw.getChildAt(i); TextView tv = ((TextView) lLayout.getChildAt(i)); tv.append(linea); }
但这样做是将“linea”附加到选项卡的名称(指示符).我想将它添加到该选项卡的内容,但我找不到一种方法来检索与之关联的TextView.
任何想法将非常感谢!
———编辑———
虽然在下面的评论中我发布了我已经有解决方案,但我发现它无效.代码现在看起来像这样:
for (int i = 0; i < tw.getChildCount(); i++) { LinearLayout LLayout = (LinearLayout) tw.getChildAt(i); TextView currenttab = (TextView) LLayout.getChildAt(0); if (tab.equals(currenttab.getText())) { th.setCurrentTab(i); TextView tabContent = (TextView) th.getTabContentView().getChildAt(0); tabContent.append(linea); return true; } }
问题是getTabContentView()仅引用当时活动的Tab,所以如果我想在未选择的选项卡的TextView中添加一些文本,我必须先使用.setCurrentTab(i)选择它,这意味着我要改变活动标签……
我认为这比我想的要容易得多……对此有何帮助?
谢谢!
———编辑———
那时我意识到无法访问非活动选项卡的内容.至少不那样.据说您无法访问非活动选项卡的视图,因为它没有关联的视图,至少不是公共视图.
我试图声明一个Map,其中第一个String是选项卡的名称,第二个是与其内容关联的TextView,并在创建选项卡时分配它们.结果是我只能引用活动选项卡的TextView,其他引发了NullPointer异常.
所以我把问题集中在一起.现在,如果我必须将一些文本添加到非活动选项卡中,我将该文本添加到哈希,我声明了一个OnTabchangelistener,每次我更改选项卡时,我都会将待处理内容转储到其中.
这不是最出色的解决方案,但它是我工作的唯一一个……所以如果有人有更好的解决方案,我将不胜感激.
解决方法
yourTabHost.getTabWidget().getChildTabViewAt(position);
此代码返回任何选项卡的视图,甚至是非活动选项卡.
android fragmenttabhost 如何让tabs位于底部(个人笔记)
方法一:
修改布局文件
<android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" <!--这个id很特别--> android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.LinearLayoutCompat android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@android:id/tabcontent" <!--这个id很特别--> android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> <!--这个权重很特别--> <TabWidget android:id="@android:id/tabs" <!--这个id很特别--> android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0"/> <!--这个权重很特别--> </android.support.v7.widget.LinearLayoutCompat> </android.support.v4.app.FragmentTabHost>
代码如下:
mTabHost = findViewById(android.R.id.tabhost); mTabHost.setup(this,getSupportFragmentManager(),android.R.id.tabcontent); mTabHost.addTab(mTabHost.newTabSpec("First").setIndicator("First"), ThirdFragment.class,null); mTabHost.addTab(mTabHost.newTabSpec("Second").setIndicator("Second"), FourthFragment.class,null); mTabHost.addTab(mTabHost.newTabSpec("Third").setIndicator("Third"), ThirdFragment.class,null); mTabHost.addTab(mTabHost.newTabSpec("Fourth").setIndicator("Fourth"), FourthFragment.class,null);
方法二:
https://github.com/jessicass/fragment2/blob/master/res/layout/activity_main.xml
android Google Map不会在TabHost中加载
经过几天的恐慌和沮丧的想法我的API密钥是错误的,我发现我的MapView只会在我的应用程序中加载它的主要内容.
每次我试图将它附加到TabHost或尝试通过getDecorView()在已存在的视图中加载它时,地图不会加载,我只得到一张空白地图.
在TabHost中使用对我来说非常重要.
有人知道如何解决它或为什么会发生这种情况?
解决方法:
最终托管MapView的活动必须是MapActivity,至少在Android的Maps插件的当前版本中.
这可能会导致混淆或彻头彻尾的问题,在这种情况下,您尝试将活动变为其他内容.
例如,有些人尝试使用tabactivity,并使用活动作为选项卡的内容.现在已经正式弃用,恕我直言从来就不是一个特别好的模特.说到地图,即使您尝试加载到选项卡中的活动是MapActivity,这并不重要 – 包含选项卡的活动必须是MapActivity.常见的解决方法是在MapActivity中使用TabHost和TabWidget.虽然这确实需要调用TabHost上的setup(),并且你丢失了一些辅助方法,但它会遇到这个问题.
android tabhost
结构
继承关系
public class TabHost extends FrameLayout implements ViewTreeObserver.OnTouchModeChangeListener
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.FrameLayout
android.widget.TabHost
提供选项卡(Tab页)的窗口视图容器。此对象包含两个子对象:一组是用户可以选择指定Tab页的标签;另一组是FrameLayout用来显示该Tab页的内容。个别元素通常控制使用这个容器对象,而不是设置在子元素本身的值。
(译者注:即使使用的是单个元素,也最好把它放到容器对象ViewGroup里)
内部类
interface TabHost.OnTabChangeListener
接口定义了当选项卡更改时被调用的回调函数
interface TabHost.TabContentFactory
当某一选项卡被选中时生成选项卡的内容
class TabHost.TabSpec
单独的选项卡,每个选项卡都有一个选项卡指示符,内容和tag标签,以便于记录.
公共方法
public void addTab (TabHost.TabSpec tabSpec)
新增一个选项卡
参数
tabSpec 指定怎样创建指示符和内容.
public void clearAllTabs ()
从tab widget中移除所有关联到当前tab host的选项卡
public boolean dispatchKeyEvent (KeyEvent event)
分发按键事件到焦点传递路线上的下一视图。焦点传递路线从视图树的顶层开始一直到当前获取焦点的视图停止。如果此视图已经获取焦点,将分发给它自身。否则,将分发到焦点传递路线的下一节点。此方法会触发任何一个按键监听器.
(译者注:关于focus path,可以参考以下地址:
http://blog.csdn.net/maxleng/archive/2010/05/04/5557758.aspx)
参数
event 分发的按键事件
返回值
如果事件已经处理则返回true,否则返回false.
public void dispatchWindowFocusChanged (boolean hasFocus)
当窗口包含的此视图获取或丢失焦点时触发此方法.ViewGroups应该重写以路由到他的子元素
参数
hasFocus 如果窗口包含的此view依获取焦点,返回true,否则返回false.
public int getCurrentTab ()
(译者注:获取当前选项卡的id)
public String getCurrentTabTag ()
(译者注:当前选项卡的Tag标签内容)
public View getCurrentTabView ()
(译者注:获取当前选项卡的视图view)
public View getCurrentView ()
(译者注:获取当前的视图view)
public FrameLayout getTabContentView ()
获取保存tab内容的FrameLayout
public TabWidget getTabWidget ()
(译者注:根据系统规定的id:tabs来找到TabWidget,并返回,注意,这里的ID必须是tabs。源代码中表示如下:
private TabWidget mTabWidget;
mTabWidget=(TabWidget)findViewById(com.android.internal.R.id.tabs);)
public TabHost.TabSpec newTabSpec (String tag)
获取一个新的TabHost.TabSpec,并关联到当前tab host
参数
tag 所需的选项卡标签(tag)
public void onTouchModeChanged (boolean isInTouchMode)
当触摸模式发生改变时调用的回调函数.
参数
isInTouchMode 如果视图结构当前处于触摸模式,返回true,否则返回false.
public void setCurrentTab (int index)
(译者注:设置当前的选项卡
参数
Index 为当前选项卡的索引。)
public void setCurrentTabByTag (String tag)
(译者注:根据选项卡的Tab标签来设置当前的选项卡
参数
tag 想要被设置为当前选项卡的tag标签值。)
public void setOnTabChangedListener (TabHost.OnTabChangeListener l)
注册一个回调函数,当选项卡中的任何一个tab的选中状态发生改变时调用。
(译者注:setCurrentTab(index)时会触发调用)
参数
l 将运行的回调函数
public void setup ()
如果使用findViewById()加载TabHost,那么在新增一个选项卡tab之前,需要调用setup()。然而,当你在一个TabActivity里使用getTabHost()获取TabHost,你就不再需要调用setup()了。(译者注:实现tab窗口的两种方法:继承activity时,使用findViewById()查找TabHost,然后调用setup();继承TabActivity,通过getTabHost()查找,此时不用调用setup())例子:
public void setup (LocalActivityManager activityGroup)
如果你使用setContent(android.content.Intent),那么当activityGroup用于加载本地activity之时,必须调用此方法。如果你拓展(继承)TabActivity将自动调用setup()方法。
参数
activityGroup 用来为选项卡内容加载activities的ativityGroup
受保护方法
protected void onAttachedToWindow ()
当视图附加到窗口上时被调用。在这个点的表面进行绘制。注意此函数确保在onDraw(Cancas)之前调用,然而它可能在第一次执行onDraw之前的任何时间被调用——包括的onMeasure(int,int)的之前或之后。
protected void onDetachedFromWindow ()
当视图从窗口分离时被调用。在这个点的表面不再有画面绘制。
============================================================================
TabHost广泛运用于android程序中,在程序中运用TabHost,解决了手机屏幕小,显示内容少的问题,如系统自带的拨号程序,就用了TabHost
分为拨号、通话纪录、联系人、收藏几个Tab。
定义布局文件:tabhost.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp">
<LinearLayout
android:id="@+id/tab_edit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical">
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入检索关键字..."
android:textSize="18sp"/>
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab_clock"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical">
<AnalogClock
android:id="@+id/myAnalogClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab_sex"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical">
<RadioGroup
android:id="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/male"
android:checked="true"
android:text="性别:男"/>
<RadioButton
android:id="@+id/female"
android:text="性别:女"/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
</ScrollView>
2.java代码:
package com.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.test.R;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.ToggleButton;
public class TabHostDemo extends TabActivity {
//自定义
private TabHost mytabhost = null; // 定义TabHost
private int[] layRes = { R.id.tab_edit, R.id.tab_clock , R.id.tab_sex };
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mytabhost = super.getTabHost() ; // 取得TabHost对象
LayoutInflater.from(this) // 取得LayoutInflater对象
.inflate(R.layout.tabhost, // 定义转换的布局管理器
this.mytabhost.getTabContentView(), // 指定标签增加的容器
true); // 实例化布局管理器中的组件
for (int x = 0; x < this.layRes.length; x++) { // 循环取出所有布局标记
TabSpec myTab = mytabhost.newTabSpec("tab" + x); // 定义TabSpec
myTab.setIndicator("标签 - " + x) ; // 设置标签文字
myTab.setContent(this.layRes[x]) ; // 设置显示的组件
this.mytabhost.addTab(myTab) ; // 增加标签
}
}
}
3.执行结果:
=========================================================================
案例二:
http://www.eoeandroid.com/thread-1035-1-1.html
Android TabHost Auto在TabChange的侧面ScrollView中垂直滚动?
我在我的Activity中使用ScrollView中的TabHost但是当我选择tab时它会自动垂直滚动我的视图结束.
解决方法:
在这种情况下,子视图由于向上滚动而获得焦点.
要解决此问题,您需要创建扩展ScrollView的自定义ScrollView.
代码片段看起来像这样.
public class MyScrollView extends ScrollView {
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void requestChildFocus(View child, View focused) {
// if (focused instanceof TabHost) // here
return;
//super.requestChildFocus(child, focused);
// here you need to return instead of **super.requestChildFocus(child, focused);**
}
和xml看起来像这样
<com.views.widget.MyScrollView
android:focusable="false"
android:focusableInTouchMode="false"
android:id="@+id/root_scroll_view"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="wrap_content">
</com.views.widget.MyScrollView >
今天关于android – 如何在TabHost中获取Tab的内容?和android获取topactivity的分享就到这里,希望大家有所收获,若想了解更多关于android fragmenttabhost 如何让tabs位于底部(个人笔记)、android Google Map不会在TabHost中加载、android tabhost、Android TabHost Auto在TabChange的侧面ScrollView中垂直滚动?等相关知识,可以在本站进行查询。
本文标签: