[转载]Android的tabhost取消默认的白底色 - - ITeye技术网站

mikel阅读(963)

[转载]tabhost取消默认的白底色 – – ITeye技术网站.

http://www.cnblogs.com/zhaolong_chn/archive/2011/12/18/2291872.html
http://www.cnblogs.com/wader2011/archive/2011/10/13/2209668.html
http://www.cnblogs.com/zhukoo/archive/2011/09/30/2196878.html

package com.hg.beyond.demo.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

import com.hg.beyond.demo.R;

public class PhoneManagerHomeActivity extends TabActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.phone_manager_home);
		setTheme(android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
		launchTabHost();
	}

	private void launchTabHost() {

		final TabHost tabHost = this.getTabHost();
		
		
		//注册监听器调用方法改变底色
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				updateTabBackground(tabHost);

			}
		});
		View callView = LayoutInflater.from(this).inflate(
				R.layout.phone_manager_tab_item, null);

		View msgView = LayoutInflater.from(this).inflate(
				R.layout.phone_manager_tab_item, null);

		View contactView = LayoutInflater.from(this).inflate(
				R.layout.phone_manager_tab_item, null);

		View favoritView = LayoutInflater.from(this).inflate(
				R.layout.phone_manager_tab_item, null);

		TextView callLable = (TextView) callView
				.findViewById(R.id.tab_item_lable);
		ImageView callImage = (ImageView) callView
				.findViewById(R.id.tab_item_icon);
		callLable.setText(getString(R.string.tab_call_txt));
		callImage.setImageResource(R.drawable.call_tab_item_selector);

		TextView contactLable = (TextView) contactView
				.findViewById(R.id.tab_item_lable);
		ImageView contactImage = (ImageView) contactView
				.findViewById(R.id.tab_item_icon);
		contactLable.setText(getString(R.string.tab_contact_txt));
		contactImage.setImageResource(R.drawable.contact_tab_item_selector);

		TextView msgLable = (TextView) msgView
				.findViewById(R.id.tab_item_lable);
		ImageView msgImage = (ImageView) msgView
				.findViewById(R.id.tab_item_icon);
		msgLable.setText(getString(R.string.tab_msg_txt));
		msgImage.setImageResource(R.drawable.msg_tab_item_selector);

		TextView favoritLable = (TextView) favoritView
				.findViewById(R.id.tab_item_lable);
		ImageView favoritImage = (ImageView) favoritView
				.findViewById(R.id.tab_item_icon);
		favoritLable.setText(getString(R.string.tab_fav_txt));
		favoritImage.setImageResource(R.drawable.fav_tab_item_selector);

		tabHost.addTab(tabHost.newTabSpec("call").setIndicator(callView)
				.setContent(new Intent(this, CallActivity.class)));

		tabHost.addTab(tabHost.newTabSpec("msg").setIndicator(msgView)
				.setContent(new Intent(this, MsgActivity.class)));

		tabHost.addTab(tabHost.newTabSpec("contact").setIndicator(contactView)
				.setContent(new Intent(this, ContactActivity.class)));

		tabHost.addTab(tabHost.newTabSpec("favorit").setIndicator(favoritView)
				.setContent(new Intent(this, FavoritActivity.class)));
		tabHost.setCurrentTab(0);
	}

	/**
	 * 更新Tab标签的底色图
	 * 
	 * @param tabHost
	 */
	private void updateTabBackground(final TabHost tabHost) {
		for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
			View vvv = tabHost.getTabWidget().getChildAt(i);
			if (tabHost.getCurrentTab() == i) {
				
				// 选中后的背景
				vvv.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.cm_tabwidget_select_bg));
			} else {
				// 非选择的背景
			 vvv.setBackgroundDrawable(null);
			}
		}
	}

}

[转载]Android设置TabHost的透明标签-Android开发问题解答-eoe Android开发者社区_Android开发论坛

mikel阅读(848)

[转载]设置TabHost的透明标签-Android开发问题解答-eoe Android开发者社区_Android开发论坛.

图1
图2
理想的情况应该是图1的效果,但是发现background中使用png图片默认还是会有黑色底色,请问应该如何达到透明的效果
布局如下

<!--?xml version="1.0" encoding="utf-8"?-->

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

代码如下

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
 
public class TabHostActivity extends TabActivity implements
                OnCheckedChangeListener, OnFocusChangeListener
{
        private TabHost tabHost;
        private RadioGroup tabs;
        private RadioButton btn_login, btn_register, btn_forgot;
 
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.tabhost_layout);
 
                tabs = (RadioGroup) findViewById(R.id.main_tabs);
                btn_login = (RadioButton) findViewById(R.id.tab_radio01);
                btn_register = (RadioButton) findViewById(R.id.tab_radio02);
                btn_forgot = (RadioButton) findViewById(R.id.tab_radio03);
 
                btn_login.setOnFocusChangeListener(this);
                btn_register.setOnFocusChangeListener(this);
                btn_forgot.setOnFocusChangeListener(this);
 
                tabs.setOnCheckedChangeListener(this);
 
                tabHost = getTabHost();
 
                tabHost.addTab(tabHost
                                .newTabSpec("login")
                                .setIndicator(getResources().getString(R.string.login),
                                                getResources().getDrawable(R.drawable.login))
                                .setContent(new Intent(this, LoginActivity.class)));
 
                tabHost.addTab(tabHost
                                .newTabSpec("register")
                                .setIndicator(getResources().getString(R.string.register),
                                                getResources().getDrawable(R.drawable.register))
                                .setContent(new Intent(this, RegisterActivity.class)));
 
                tabHost.addTab(tabHost
                                .newTabSpec("forgot")
                                .setIndicator(getResources().getString(R.string.forgotPass),
                                                getResources().getDrawable(R.drawable.forgot_password))
                                .setContent(new Intent(this, ForgotPassActivity.class)));
        }
 
        @Override
        public void onFocusChange(View v, boolean hasFocus)
        {
                switch (v.getId())
                {
                case R.id.tab_radio01:
 
                        break;
                case R.id.tab_radio02:
 
                        break;
                case R.id.tab_radio03:
 
                        break;
                }
        }
 
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId)
        {
                switch (checkedId)
                {
                case R.id.tab_radio01:
                        tabHost.setCurrentTabByTag("login");
                        break;
                case R.id.tab_radio02:
                        tabHost.setCurrentTabByTag("register");
                        break;
                case R.id.tab_radio03:
                        tabHost.setCurrentTabByTag("forgot");
                        break;
                }
        }
}

[转载]Eclipse安装SVN插件方式简明介绍 - 51CTO.COM

mikel阅读(1068)

[转载]Eclipse安装SVN插件方式简明介绍 – 51CTO.COM.

本节和大家一起学习一下Eclipse安装SVN插件方式,主要从三个方面来讲解,在这里和大家分享一下,希望大家通过本节的介绍对Eclipse安装SVN插件有一定的认识。下面是具体介绍。

一、Links安装:

推荐使用此种安装方式,因为它便于插件的管理。

    1. 在eclipse根目录下新建文件夹links,这样就得到了eclipse\links
    2. 在eclipse\links下新建一个link文件(需要为每个插件建一个.link文件),比如svn.link,在这里我的插件安装目录结构为F:\eclipsePlugins\svn\,所以link文件名我命名为svn.link
    3. 在svn.link中写入如下一句话path=F:/EclipsePlugins/svn,或者path=F:\\EclipsePlugins\\svn。即path=你的插件安装目录(代表eclipse去哪个路径加载查件)

svn下的目录结构为

svn

+eclipse

+plugins

+fearture

  1. 重新启动eclipse(如果你的Eclipse安装SVN插件之前启动过需要先把eclipse关掉)

注意:路径的写法使用“/”或者“\\”而不是用“\”取代。此处容易出错。

Important:如果发现插件没有安装成功,则删除eclipse目录下的configuration目录中的文件夹 org.eclipse.update(这是一个记录插件更新情况的文件夹,其中的platform.xml文件是当前使用的插件),删除掉这个文件夹 后,eclipse会重新扫描所有的插件,此时再重新启动eclipse时可能会比刚才稍微慢点。

二、在线安装用升级的方式—-Eclipse平台本身的更新管理器来实现

    1. eclipse菜单栏help->softwareupdates->FindandInstall
    2. 选择searchfornewfeaturestoinstall->Next
    3. 选择NewRemoteSite->输入如下:->OK

name:subclipse     url:http://subclipse.tigris.org/update_1.4.x

  1. 选上subclipse插件,点击Finish就可以了。
  2. 注意在这里也可以重新选择plugins的位置,一般习惯集中在一个文件夹(比如上面的EclipsePlugins文件夹)来管理自己安装的plugins
  3. FINISH,它会自动的从指定资源下去找安装的插件。

三、插件地址:

    1. SVN:http://subclipse.tigris.org/update_1.4.x

记录站点的文件:workspace\.metadata\.plugins\org.tigris.subversion.subclipse.core\.svnProviderState

    1. GWT:

Eclipse3.5(Galileo)http://dl.google.com/eclipse/plugin/3.5

Eclipse3.4(Ganymede)http://dl.google.com/eclipse/plugin/3.4

Eclipse3.3(Europa)http://dl.google.com/eclipse/plugin/3.3

    1. implementors:查找接口实现类的Eclipse插件implementors

Eclipse安装SVN插件功能:在eclipse中查找并定位类时,可以选择直接定位到接口或者定位到接口的实现。

Eclipse安装SVN插件使用方法:

选择类(接口)或者类(接口)的方法,单击右键,选择OpenInterface,直接定位到接口;选择OpenImplementation,直接

定位到实现类,如果有多个实现类就会出现一个选择框,选择具体的某个实现类。

插件主页:http://eclipse-tools.sourceforge.net/implementors/

    1. Eclipse文件改变编码插件http://blog.csdn.net/shangpusp/archive/2010/04/08/5461825.aspx
    2. EasyExplorer:快速打开文件所在文件夹。

下载:http://sourceforge.net/projects/easystruts/

介绍:http://eclipse-plugins.2y.net/eclipse/plugin_details.jsp;jsessionid=4714380194D92E902C843E3D5F82C4BB?id=192

更多:http://eclipse-plugins.2y.net/eclipse/plugins.jsp?category=Tools+-+ContextMenu

CSS插件、JS和AS插件、XML插件、正则表达式测试插件。本节关于Eclipse安装SVN插件方式介绍完毕。

[转载]安装Eclipse SVN plugin(Subclipse) - Guoyaxu - ITeye技术网站

mikel阅读(944)

[转载]安装Eclipse SVN plugin(Subclipse) – Guoyaxu – ITeye技术网站.

这里介紹Eclipse的SVN Plugin,叫做Subclipse,官方网站的下载地址在http://subclipse.tigris.org/,也提供了update site http://subclipse.tigris.org/update
Eclipse 的使用者可以通过Eclipse的插件自动下载和更新功能来安装这个插件,在Eclipse的菜单中选择Help->Software Updates->Find and Install-> Search for new features to install ->New Remote Site。URL中就输入http://subclipse.tigris.org/update,Eclipse就会自已安装上了.

安 装完成后,在Eclipse的plugins中就会多了5个包,命名为org.tigris.subversion.*的都应该是了。打开 Eclipse,window->show view窗口中多了一个SVN文件夹,到此就证明svn插件成功的安装上了。具体的使用方法,在Eclipse的Help中有详细的帮助 Subclipse – Subversion Eclipse Plugin,教你一步一步的使用SVN的客户端了。如果你对subversion想进行深入的了解,那么看看help中的Version Control with Subversion一定有所收获,通过本人的观察,这份文档和sbuversion安装文件中提供的官方文档一样,这里看起来就更舒服些了。

Android的TabHost使用笔记

mikel阅读(950)

Android的TabHost使用笔记

    1. Activity继承自TabAcitvity的使用,目前TabActivity不推荐使用,官方已经不建议使用了
public class MainTabActivity extends TabActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		TabHost tabHost = getTabHost();
		LayoutInflater.from(this).inflate(R.layout.tab1,
				tabHost.getTabContentView(), true);
		LayoutInflater.from(this).inflate(R.layout.tab2,
				tabHost.getTabContentView(), true);
		tabHost.addTab(tabHost.newTabSpec("TAB1").setIndicator("线性布局")
				.setContent(R.id.layout01));
		tabHost.addTab(tabHost.newTabSpec("TAB2").setIndicator("相对布局")
				.setContent(R.id.layout03));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main_tab, menu);
		return true;
	}

}
      xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainTabActivity" >

</RelativeLayout>
  1. Activity直接使用TabHost,这种比较常用,不过使用过程中会出现很多问题

先上正确的使用代码:

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// //ListView
		// ListView ls=(ListView) findViewById(R.id.listView1);
		// ls.setAdapter(new SimpleAdapter(this,getData(),R.layout.goods,
		// new String[]{"title","info","img"},new
		// int[]{R.id.title,R.id.info,R.id.imageView1}
		// ) );
		// set Adapter
		SimpleAdapter adapter = new SimpleAdapter(this, getData(),
				R.layout.goods, new String[] { "title", "info", "img" },
				new int[] { R.id.title, R.id.info, R.id.imageView1 });
		setListAdapter(adapter);
		setContentView(R.layout.activity_main);
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
		// data
		// 获取TabHost
		TabHost tabHost = (TabHost) findViewById(R.id.tabs);
		tabHost.setup();
		LayoutInflater.from(this).inflate(R.layout.hometab,
				tabHost.getTabContentView(), true);
		LayoutInflater.from(this).inflate(R.layout.categorytab,
				tabHost.getTabContentView(), true);
		tabHost.addTab(tabHost.newTabSpec("parent").setIndicator("首页",this.getResources().getDrawable(R.drawable.home_basic))
				.setContent(R.id.hometab));
		tabHost.addTab(tabHost.newTabSpec("catetory").setIndicator("分类",this.getResources().getDrawable(R.drawable.book_bookmark))
				.setContent(R.id.catetorytab));
}

xml代码:

    <TabHost
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white" >

        <LinearLayout
            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_weight="1">
            </FrameLayout>
        </LinearLayout>
    </TabHost>

[/java]

常见问题:

1.TabHost的Tab内容显示其他xml页面进行显示,结果提示id找不到,

错误提示:Could not create tab content because could not find view with id

原因:直接使用


        TabSpec homeSpec = tabHost.newTabSpec("parent");
         homeSpec.setIndicator("首页",
         this.getResources().getDrawable(R.drawable.home_basic));
         homeSpec.setContent(R.id.catetorytab);//这样赋值xml的视图ID,不能获取到值,需要先用LayoutInflater进行xml文件的解析并赋值给tabhost

代码如下:

tabHost.Setup();//注意必须在设置之下写

LayoutInflater.from(this).inflate(R.layout.categorytab,
                tabHost.getTabContentView(), true);

[转载]Android-LayoutInflater 的应用-Android开发实例教程-eoe Android开发者社区_Android开发论坛

mikel阅读(955)

[转载]Android-LayoutInflater 的应用-Android开发实例教程-eoe Android开发者社区_Android开发论坛.

        在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater 是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如 Button、TextView等)。
具体作用:
1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。

LayoutInflater 是一个抽象类,在文档中如下声明:

Java代码

  • public abstract class LayoutInflater extends Object

public abstract class LayoutInflater extends Object   获得 LayoutInflater 实例的三种方式:
Java代码

  • 1. LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()
  • 2. LayoutInflater localinflater =  (LayoutInflater)context.getSystemService
  •                                                  (Context.LAYOUT_INFLATER_SERVICE);
  • 3. LayoutInflater inflater = LayoutInflater.from(context);

1. LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()2. LayoutInflater localinflater =  (LayoutInflater)context.getSystemService                                                 (Context.LAYOUT_INFLATER_SERVICE);3. LayoutInflater inflater = LayoutInflater.from(context);     其实,这三种方式本质是相同的,从源码中可以看出:

getLayoutInflater():

Activity 的 getLayoutInflater() 方法是调用 PhoneWindow 的getLayoutInflater()方法,看一下该源代码:

Java代码

  • public PhoneWindow(Context context) {
  •         super(context);
  •         mLayoutInflater = LayoutInflater.from(context);
  • }

public PhoneWindow(Context context) {          super(context);          mLayoutInflater = LayoutInflater.from(context);  }   可以看出它其实是调用 LayoutInflater.from(context)。
LayoutInflater.from(context):
Java代码

  • public static LayoutInflater from(Context context) {
  •     LayoutInflater LayoutInflater =
  •             (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  •     if (LayoutInflater == null) {
  •         throw new AssertionError(“LayoutInflater not found.”);
  •     }
  •     return LayoutInflater;
  • }

public static LayoutInflater from(Context context) {       LayoutInflater LayoutInflater =               (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);       if (LayoutInflater == null) {           throw new AssertionError(“LayoutInflater not found.”);       }       return LayoutInflater;   }

可以看出它其实调用 context.getSystemService()。

 

结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。

 

inflate 方法
通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下:

Java代码

  • public View inflate (int resource, ViewGroup root)
  • public View inflate (XmlPullParser parser, ViewGroup root)
  • public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)
  • public View inflate (int resource, ViewGroup root, boolean attachToRoot)

public View inflate (int resource, ViewGroup root)  public View inflate (XmlPullParser parser, ViewGroup root)    public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)    public View inflate (int resource, ViewGroup root, boolean attachToRoot)  1:
  public View inflate (int resource, ViewGroup root)
reSource:View的layout的ID
root:如果为null,则将此View作为根,此时既可以应用此View中的其他控件了。
如果!null,  则将默认的layout作为View的根。

2:
  public View inflate ( XmlPullParser parser, ViewGroup root)
parser:你需要解析xml的解析接口
root:如果null,则将此View作为根,此时既可以应用此View中的其他控件了。
如果!null, 则将默认的layout作为View的根。

3:
  public View inflate ( XmlPullParser parser, ViewGroup root, boolean attachToRoot)
parser:你需要解析View的xml的解析接口
root:如果null,则将此View作为根,此时既可以应用此View中的其他控件了。
如果!null, 则将默认的layout作为View的根。
attachToRoot:
ture:也就将此解析的xml作为View根
fase:则为默认的xml,做为根视图View

4:
  public View inflate (int resource, ViewGroup root, boolean attachToRoot)

  resource:View的layout的ID
  root:如果null,则将此View作为根,此时既可以应用此View中的其他控件了。
           如果!null, 则将默认的layout作为View的根。
  attachToRoot:
  ture:也就将此解析的xml作为View根
fase:则为默认的xml,做为根视图View

示意代码:
Java代码

  • LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
  • View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));
  • //EditText editText = (EditText)findViewById(R.id.content);// error
  • EditText editText = (EditText)view.findViewById(R.id.content);

LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);    View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));    //EditText editText = (EditText)findViewById(R.id.content);// error  EditText editText = (EditText)view.findViewById(R.id.content);  同时在此讲讲让我去API中去理解这四个函数的原因吧!嘿嘿!你肯定又会多学一招!
在Activity中:
大家是否知道,在setContentView(new MySurfaceView(this))后,此Activity中声明的View控件,
如:TextView 为什么引用不到layout布局文件中的控件ID呢!初一看能够应用到,但是为什么编译就报空指针呢!原因:在setContentView(new MySurfaceView(this))后,此时的View变为了根视图了,虽然能应用到TextView对应的ID,但是我在 MySurfaceView中根本就没有这个对象,所以就报空指针咯!解决办法:
View view = LayoutInflater.from(this).inflate(R.layout.passover, null);注:每解析一次都会产生不同的对象
然后你再引用没问题,使用自如了。

另外getSystemService()是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。

传入的Name 返回的对象 说明
WINDOW_SERVICE WindowManager 管理打开的窗口程序
LAYOUT_INFLATER_SERVICE LayoutInflater 取得xml里定义的view
ACTIVITY_SERVICE ActivityManager 管理应用程序的系统状态
POWER_SERVICE PowerManger 电源的服务
ALARM_SERVICE AlarmManager 闹钟的服务
NOTIFICATION_SERVICE NotificationManager 状态栏的服务
KEYGUARD_SERVICE KeyguardManager 键盘锁的服务
LOCATION_SERVICE LocationManager 位置的服务,如GPS
SEARCH_SERVICE SearchManager 搜索的服务
VEBRATOR_SERVICE Vebrator 手机震动的服务
CONNECTIVITY_SERVICE Connectivity 网络连接的服务
WIFI_SERVICE WifiManager Wi-Fi服务
TELEPHONY_SERVICE TeleponyManager 电话服务

 

[转载]Android TabHost Exception could not create tab content because could not find view with id_ljlkings的空间_百度空间

mikel阅读(834)

[转载]Android TabHost Exception_ljlkings的空间_百度空间.

小弟不才,今日自学Android,学习其中的tab分页,这个确实是一个很不错的布局方式。

 

其中小弟遇到其中的一些异常,以及解决方式,分享给大家。

 

问题①: ‍Your content must have a TabHost whose id attribute is ‘Android.R.id.tabhost’

解决方法:(原帖)‍http://morry.javaeye.com/blog/744598

就是把以上帖中的红字的地方按照Android的规定方式写。照着以上帖子抄就行啦、

 

问题②: Android Exception:could not create tab content because could not find view with id 2131034115(代码可能不一样)

解决方法:(原帖)‍http://hi.baidu.com/ljlkings/blog/item/47f1afdbdcd27de638012f76.html

帖子的意思是:‍需要用LayoutInflater过滤出布局来,给TabHost加上含有Tab页面的FrameLayout。

 

具体说明如下:

LayoutInflater的作用就是把xml文件转化为view,刚好把not find 的 view 找出来。

 

这里有个小问题要注意的。就是我们一般都很习惯性的在oncreat()里面 加上this.setContent(R.layout.main)。其实是不需要的,因为在tab的布局是通过LayouInfater来设置的。

 

同时有一篇好的帖子提供大家参考的,非常适合像我这种菜鸟入门的:

‍http://www.eoeandroid.com/thread-1035-1-1.html

Android Exception: Did you forget to call ‘public void setup (LocalActivityManager activityGroup)’

Answer:you need change MainActivity’s base class from Activity to ActivityGroup, as follows:
public class MainActivity extends ActivityGroup {

}

ActivityGroup will take care of an instance of LocalActivityManager. So you don’t need to create it. After the base class is changed, just call getLocalActivityManager() function defined in the base class to get that instance. Call tabHost’s setup function like this:
tabHost.setup(this.getLocalActivityManager()); Android Exception:could not create tab content because could not find view with id 2131034115:Answer:you may forget the following sentence:LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.test, tab_host.getTabContentView(), true);r.layout.test is the content that you want to display in a tab.Good Luck for you in your programming.

[转载]Android自定义TabHost控件 - _Android_红颜 - eoe移动开发者社区

mikel阅读(827)

[转载]自定义TabHost控件 – _Android_红颜 – eoe移动开发者社区.

今天学习了一下 TabHost以前也是一知半解 今天想系统了解下 JAVA代码非常简单 xml只是2个简单布局 这里就不列出了

 

TabHost tabHost = (TabHost)findViewById(R.id.tabHost);
        tabHost.setup();
        LayoutInflater inflater = LayoutInflater.from(this);
        inflater.inflate(R.layout.tab1, tabHost.getTabContentView());
        inflater.inflate(R.layout.tab2, tabHost.getTabContentView());
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("TAB1").setContent(R.id.tab1));
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("TAB2").setContent(R.id.tab2));

学习过程中遇到了这样的错误 在setContent里 我添入的是 layout的ID 导致运行出错
Could not create tab content because could not find view with id 2130903041
这里是我的不仔细 查了很久发现原来 这里serContent()里边的要是视图的id,比如
LinearLayout的ID 这样的,结果把2个xml文件里的Layout加上id再载入就OK了,这里要
反思一下 为什么会出现这种细节错误

底部标签的实现:
其实也很简单,就是xml里改一下 2个属性

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

这里FrameLayout要写在上边 weight要设为1 然后还要在外边加一层垂直布局的LinearLayout

[转载]Android选项卡TabHost的研究(一)创建选项卡有几种方式? - Android火鸟 - 博客园

mikel阅读(947)

[转载]选项卡的研究(一)创建选项卡有几种方式? – Android火鸟 – 博客园.

选项卡的创建有几种方式呢?

第一种,继承TabActivity,TabActivity类似于ListActivity,这样我们可以不用指定xml布局文件,系统会提供默 认的xml布局。说明一点,我们也可以自己配置xml布局文件(但是不能改变TabHost和TabWidget,TabContent的id名称),将 选项卡放置在屏幕的下方。

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
//创建一个容器
TabHost.TabSpec tabSpec = tabHost.newTabSpec(“FIRST”);//picture是这个标签的唯一标识
//设置这个标签的内容
tabSpec.setContent(this);
//设置这个标签的lable,也就是这个标签的选项卡上显示的文字
tabSpec.setIndicator(“FIRST”);
//将这个标签添加到标签容器中
tabHost.addTab(tabSpec);
TabHost.TabSpec tabSpceSecond = tabHost.newTabSpec(“SECOND”);
tabSpceSecond.setContent(this);
tabSpceSecond.setIndicator(“SECOND”);
tabHost.addTab(tabSpceSecond);
tabHost.setCurrentTab(0);

}

 

需要另外注意的一点:

tabSpec.setContent(***);方法必须要调用,否则会报异常。setContent方法一共有三个重载,

//这个主要用于自己配置xml布局,并且在FrameLayout里面配置了view,作为TabHost的显示内容

public TabSpec setContent(int viewId) {
mContentStrategy = new ViewIdContentStrategy(viewId);
return this;
}

//这种主要适用于继承TabActivity,并且使用系统自带的xml布局文件

public TabSpec setContent(TabContentFactory contentFactory) {
mContentStrategy = new FactoryContentStrategy(mTag, contentFactory);
return this;
}

//  //这种主要适用于继承TabActivity,并且使用系统自带的xml布局文件
public TabSpec setContent(Intent intent) {
mContentStrategy = new IntentContentStrategy(mTag, intent);
return this;
}

提个小问题,我们知道,在TabHost中用于存储显示view的控件时FrameLayout,对于帧布局来说,

<FrameLayout Android:id=”@Android:id/tabcontent”
Android:layout_width=”fill_parent” android:layout_height=”wrap_content”
android:padding=”5dp” android:layout_weight=”1″>
<TextView android:id=”@+id/first” android:layout_width=”fill_parent”
android:layout_height=”fill_parent” android:text=”fist” />
<TextView android:id=”@+id/second” android:layout_width=”fill_parent”
android:layout_height=”fill_parent” android:text=”second” />
</FrameLayout>

第二个TextView会将第一个TextView覆盖,那么TabHost又是如何实现点击不同的选项卡显示不同的view的呢?

1. public TabSpec setContent(int viewId) {
mContentStrategy = new ViewIdContentStrategy(viewId);
return this;
}

ViewIdContentStrategy:这个类的作用是创建显示View,并将其设为gone,

2. private ViewIdContentStrategy(int viewId) {
mView = mTabContent.findViewById(viewId);
if (mView != null) {
mView.setVisibility(View.GONE);
} else {
             throw new RuntimeException(“Could not create tab content because ” +
“could not find view with id ” + viewId);
}
}

3. 在setCurrentTab方法中:

mCurrentView = spec.mContentStrategy.getContentView();

在getContentView方法中,又将这个view设为可见,即可显示在界面上。

public View getContentView() {
mView.setVisibility(View.VISIBLE);
return mView;
}

第二种,继承Activity,使用自己配置的xml布局文件,这一种不推荐使用,因为他只是将TabActivity的工作重新做了一遍。

[转载]Android中选项卡TabHost的基本使用-Android开发实例教程-eoe Android开发者社区_Android开发论坛

mikel阅读(828)

[转载]Android中选项卡TabHost的基本使用-Android开发实例教程-eoe Android开发者社区_Android开发论坛.

今天来学习一下选项卡(TabHost)的使用,
        选项卡的使用很常见,比如说:我们在手机上面 已接来电,未接来电的分组,首先来看下实现出来的效果截图:
我们要去实现TabHost,主要有两种方法:
        1、各选项内容在布局文件中定义。
2、主Activity类继承TabActivity;
3、用getTabHost()方法获取TabHost

1、直接在布局文件中定义TabHost
注意:TabWidget的id必须是@

Android:id/tabs,FrameLayout的id必须是   @Android:id/tabcontent。 


    接下来使用第一种的实现方法来去实现TabHost

    主Activity类:

package com.jiangqq.tabhost;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TabHostActivity_Second extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// setContentView(R.layout.tabhost_second);
// 得到TabHost
TabHost tabHost = this.getTabHost();

// 把自己的布局文件添加到TabHost 的FrameLayout中 【注意】很重要的一句代码
LayoutInflater.from(this).inflate(R.layout.tabhost_second,
tabHost.getTabContentView(), true);
// 设置选项卡
// 参数:是选项卡的标签
TabSpec parentSpec = tabHost.newTabSpec("parent");
parentSpec.setIndicator("基类",
this.getResources().getDrawable(R.drawable.announcements256));
parentSpec.setContent(R.id.tab_1);

TabSpec subSpec = tabHost.newTabSpec("sub");
subSpec.setIndicator("子类",
this.getResources().getDrawable(R.drawable.content256));
subSpec.setContent(R.id.tab_2);

tabHost.addTab(parentSpec);
tabHost.addTab(subSpec);

}
}

TabHost的布局文件:

package com.jiangqq.tabhost;
 
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
 
public class TabHostActivity_Second extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// setContentView(R.layout.tabhost_second);
// 得到TabHost
TabHost tabHost = this.getTabHost();
 
// 把自己的布局文件添加到TabHost 的FrameLayout中 【注意】很重要的一句代码
LayoutInflater.from(this).inflate(R.layout.tabhost_second,
tabHost.getTabContentView(), true);
// 设置选项卡
// 参数:是选项卡的标签
TabSpec parentSpec = tabHost.newTabSpec("parent");
parentSpec.setIndicator("基类",
this.getResources().getDrawable(R.drawable.announcements256));
parentSpec.setContent(R.id.tab_1);
 
TabSpec subSpec = tabHost.newTabSpec("sub");
subSpec.setIndicator("子类",
this.getResources().getDrawable(R.drawable.content256));
subSpec.setContent(R.id.tab_2);
 
tabHost.addTab(parentSpec);
tabHost.addTab(subSpec);
 
}
}

然后使用第二种方法创建TabHost
主Activity类:

package com.jiangqq.tabhost;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
 
/**
* 本例是实现TabHost----->直接在XML文件中进行配置 【注意】在xml文件中
* TahWidget和FrameLayout标签中的ID,必须要使用Android中默认的
*
* @author jiangqq
*
*/
public class TabHostActivity_First extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost_first);
// 获取TabHost
TabHost tabHost = (TabHost) findViewById(R.id.tabs);
tabHost.setup();
// 设置选项卡
// 参数:是选项卡的标签
TabSpec parentSpec = tabHost.newTabSpec("parent");
parentSpec.setIndicator("基类",
this.getResources().getDrawable(R.drawable.announcements256));
parentSpec.setContent(R.id.tab_1);
 
TabSpec subSpec = tabHost.newTabSpec("sub");
subSpec.setIndicator("子类",
this.getResources().getDrawable(R.drawable.content256));
subSpec.setContent(R.id.tab_2);
 
tabHost.addTab(parentSpec);
tabHost.addTab(subSpec);
 
}
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
 
<TabHost
android:id="@+id/tabs"
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" >
</TabWidget>
 
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
 
<TextView
android:id="@+id/tab_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="jiangqq_TabHostDemo_First" >
</TextView>
 
<TextView
android:id="@+id/tab_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="jiangqq_TabHostDemo_Second" >
</TextView>
</FrameLayout>
</LinearLayout>
</TabHost>
 
</LinearLayout>

大家可以比较一下两种方法的异同点,差异不是很大,创建起来也比较简单