android中tab选项卡怎么做
发布网友
发布时间:2022-04-29 23:11
我来回答
共1个回答
热心网友
时间:2022-06-25 12:45
第一步
res/values/strings.xml
[xhtml] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MyTabActivity!</string>
<string name="app_name">选项卡Demo</string>
<string name="andy">Andy Rubin--<a href="http://lib.csdn.net/base/15" class='replace_word' title="undefined" target='_blank' style='color:#df3434; font-weight:bold;'>Android</a>的创造者</string>
<string name="bill">Bill Joy--Java的创造者</string>
<string name="torvalds">Linus Torvalds --Linux之父</string>
</resources>
第二步
res/layout/tab_layout.xml
[xhtml] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<!--
FrameLayout:一个FrameLayout对象好比一块在屏幕上提前预定好的空白区域,
然后可以填充一些元素到里边,比方说一张图片等。
需要注意的是所有元素都被放置在FrameLayout区域的左上的区域,
而且无法为这些元素指定一个确切的位置。如果有多个元素,则后边的会重叠在前一个元素上。
android:gravity用于设置View组件的对齐方式
(另外,android:layout_gravity用于设置Container组件的对齐方式)
center_horizontal 不改变控件大小,对其到容器横向中间位置(也就是在竖直方向的中间)
android:scaleType="fitXY" 把图片不按比例来扩大或者缩小显示
-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/linearLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
>
<ImageView
android:id="@+id/imageView01"
android:layout_gravity="center"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/andy"/>
<TextView
android:id="@+id/testView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="@string/andy"
/>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout2"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
>
<ImageView
android:id="@+id/imageView02"
android:layout_gravity="center"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bill"/>
<TextView
android:id="@+id/testView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="@string/bill"
/>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout3"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
>
<ImageView
android:id="@+id/imageView03"
android:layout_gravity="center"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/torvalds"/>
<TextView
android:id="@+id/testView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="@string/torvalds"
/>
</LinearLayout>
</FrameLayout>
第三步
src/com/myandroid/tab/MyTabActivity.java
[java] view plain copy
package com.myandroid.tab;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
public class MyTabActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = this.getTabHost();
/*
* LayoutInflater的作用类似于 findViewById(),
* 不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化
* 注:findViewById()只是找控件之类(如Button和EditView)
*
* LayoutInflater.from(this)获得context实例
* 也就是相当于this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
* LAYOUT_INFLATER_SERVICE 取得xml里定义的view
*-----------------------------------------------------------------------
* getSystemService:
* 根据传入的NAME来取得对应的Object,然后转换成相应的服务对象
* android的后台运行在很多service,
* 它们在系统启动时被SystemServer开启,支持系统的正常工作,
* 比如MountService监听是否有SD卡安装及移除,ClipboardService提供剪切板功能,
* 应用程序可以通过系统提供的Manager接口来访问这些Service提供的数据
*-----------------------------------------------------------------------
*
* inflate是把xml表述的layout转化为View
* tabHost.getTabContentView() 获得Tab标签页的FrameLayout
* true表示将inflate绑定到根布局元素上
*/
LayoutInflater.from(this)
.inflate(R.layout.tab_layout,
tabHost.getTabContentView(), true);
/*
* tabHost.newTabSpec("Tab1") 创建TabHost.TabSpec,
* TabSpec即是选项卡的指示符,对于TabSpec可以设置一个标题或者设置一个标题和图标
* setIndicator 是为选项卡指示符指定一个标签和图标
* setContent 为选项卡的内容指定视图的ID
*/
tabHost.addTab(
tabHost.newTabSpec("Tab1")
.setIndicator("Tab1", getResources().getDrawable(R.drawable.png1)
).setContent(R.id.linearLayout1)
);
tabHost.addTab(
tabHost.newTabSpec("Tab2")
.setIndicator("Tab2", getResources().getDrawable(R.drawable.png2)
).setContent(R.id.linearLayout2)
);
tabHost.addTab(
tabHost.newTabSpec("Tab3")
.setIndicator("Tab3", getResources().getDrawable(R.drawable.png3)
).setContent(R.id.linearLayout3)
);
}
}
第四步
AndroidManifest.xml
[xhtml] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myandroid.tab"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyTabActivity"
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>
<uses-sdk android:minSdkVersion="8" />
</manifest>
附上出处链接:http://blog.csdn.net/jamesliulyc/article/details/6324432
如何创建tab android
在创建Tab之前,先把Tab的结构搞清楚。它的结构是这样的:最外层是一个Tabhost,Tabhost里装了些选项卡(TabSpec),每个选项卡有自己的指示符(Indicator,就是顶部可点的那个区块)和内容(Content,下半部分展示内容的区块)。现在,要做的事情就很清楚了:1、创建Tabhost 2、创建TabSpec并给TabSpec赋...
android 选项卡的标题栏怎么更改成自己的图片如图灰色的部分怎么...
用tab布局,自己定义tab标签内容
android studio怎么新建tabhost
具体如下:以下通过TabHost实现android选项卡。main.xml布局文件:<?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"> <LinearLayout android:id="@+id/t...
APP设计中的选项卡式(tab)导航
固定选项卡是安卓系统提供的三种顶级导航方式之一。它能够扁平化整个信息结构,并且支持左右滑动切换到不同的视图。 如果 需要经常切换视图 , 内容视图有限 或者需要 让用户清楚地知道可供选择的视图 ,这几种情况下可以使用固定选项卡。(4)滚动选项卡 一般滚动选项卡要比固定选项卡要...
android 怎么设置TabHost默认显示的选项卡为选中状态
可以判断当它被点击我们改变他的背景 或者背景图片为点击的 同时把其他的设置成没有被点击的背景或者图片 现在大部分都用viewpager的 viewpager实现的效果更好
android 怎么设置TabHost默认显示的选项卡为选中状态
tabhost.getCurrentTabView().setBackgroundColor(Color.CYAN); //设置默认选中状态的背景
TAB切换选项卡是什么原理
tab切换就是点击不同的标签,显示不同的内容。键盘上的Tab键位于大小写键Caps Lock键的上面,Tab键是tabulator key的缩写,其含义是作表的人,制表机,打字机上为制表用的跳格键。它最基本的用法就是用来绘制无边框的表格。单词之间的间隔都是按下一次Tab键来实现的。它一般等于8个空格的长度,当它...
pb中切换tab的选项卡tabpage
用tab_1.selecttab(n)这样就可以跳到你要显示的tab页
tab页面是什么意思?
在详细解释之前,我们首先需要了解“选项卡”这个概念。选项卡是一种常见的用户界面元素,它允许用户在同一窗口内切换不同的视图或页面。每个选项卡通常代表一个独立的页面或功能区域,用户可以通过点击选项卡来快速切换和访问这些不同的内容。Tab页面的设计有助于提高用户体验和工作效率。例如,在...
TabHost中怎么做到返回下一页面
一个切换选项卡TabHost 中有页面1,页面2,页面3,页面4。其中页面一的页面流程为登陆页面- 注册页面;如何能够在注册页面的时候还能返回到登陆页面,当我在注册页面中调用finish()函数时,整个TabHost 页面都结束了,从而程序退出了,请问如何能够做到只是把注册界面关闭了,然后就显示了上一个登陆页面呢...