实现TabHost的两种方法
来源:互联网 发布:流体计算软件app 编辑:程序博客网 时间:2024/05/20 02:27
1.通过继承Activity实现
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/hometabs" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/view1" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <TextView android:id="@+id/view2" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <TextView android:id="@+id/view3" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </FrameLayout> <TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom"> </TabWidget> </LinearLayout> </TabHost> </RelativeLayout>
MainActivity.java
package com.example.testtabhost;import android.R.integer;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TabHost;import android.widget.TabHost.OnTabChangeListener;import android.widget.Toast; public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TabHost tabHost = (TabHost) findViewById(R.id.tabhost); tabHost.setup(); tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("4").setContent(R.id.view1)); tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("5").setContent(R.id.view3)); tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("6").setContent(R.id.view2)); tabHost.setCurrentTab(1); tabHost.setOnTabChangedListener(new OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {// TODO Auto-generated method stub if (tabId.equals("tab1")) { // Intent intent =new Intent(); // intent.setClass(MainActivity.this, OtherActivity.class); Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show(); } if (tabId.equals("tab2")) { Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show(); } if (tabId.equals("tab3")) { Toast.makeText(getApplicationContext(), "3", Toast.LENGTH_SHORT).show(); }}}) ; } }2.待续
继承TabActivity实现
activity_main.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" > <ImageView android:id="@+id/tab2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/image2"/> <ImageView android:id="@+id/tab3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/image3"/> </RelativeLayout>tab1_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/image" android:padding="10dp" android:layout_width="48dp" android:layout_height="48dp" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/name" android:paddingTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/publish" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="10dp" android:gravity="right" /> </LinearLayout> <TextView android:id="@+id/content" android:paddingTop="10dp" android:paddingBottom="10dp" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>tab1_main.xml
<LinearLayout 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" > <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
MainActivity.java
package com.example.testtabhostandlistview;import android.app.TabActivity;import android.content.Intent;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.widget.TabHost; @SuppressWarnings("deprecation")public class MainActivity extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); TabHost tabHost=getTabHost(); LayoutInflater.from(this).inflate(R.layout.activity_main,tabHost.getTabContentView(), true); Intent intent=new Intent(); intent.setClass(this, Tab01Activity.class); tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("",getResources().getDrawable(R.drawable.image1)).setContent(intent)); tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("",getResources().getDrawable(R.drawable.image2)).setContent(R.id.tab2)); tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("", getResources().getDrawable(R.drawable.image3)).setContent(R.id.tab3)); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
Tab01Activity.java
package com.example.testtabhostandlistview; import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast; public class Tab01Activity extends Activity { private List<Map<String, ?>> data; private ListView listItem; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tab1_main); listItem = (ListView) this.findViewById(R.id.list); data = getData(); SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.tab1_item, new String[] { "image", "name", "publish", "content" }, new int[] { R.id.image, R.id.name, R.id.publish, R.id.content }); listItem.setAdapter(adapter); listItem.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapter, View view, int position, long id) { Map<String, Object> item=(Map<String, Object>) data.get(position); Toast.makeText(getApplicationContext(), item.get("name")+"\n\n"+item.get("content"), Toast.LENGTH_LONG).show(); } }); } private List<Map<String, ?>> getData() { List<Map<String, ?>> data = new ArrayList<Map<String, ?>>(); Map<String, Object> item = new HashMap<String, Object>(); item.put("image", R.drawable.image1); item.put("name", "世界末日"); item.put("publish", "1分钟前"); item.put("content", "我过得还可以,不好不坏,不惊不喜,一切只是还可以。这样的生活我觉得也挺好。"); data.add(item); return data; } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
0 0
- Android_UI_实现TabHost的两种方法
- Android--TabHost的两种实现方法
- Android_UI_实现TabHost的两种方法
- Android_UI_实现TabHost的两种方法
- 实现TabHost的两种方法
- TabHost两种实现方法
- TabHost两种实现方式及各个方法的作用
- tabHost的两种实现方式
- TabHost两种不同的实现方式
- 实现TabHost的两种步骤
- TabHost的两种实现方式
- TabHost两种实现方式
- 两种特殊TabHost实现
- TabHost两种实现方式
- TabHost两种实现方式
- TabHost两种实现方式
- TabHost两种实现方式
- TabHost两种实现方式
- 如何用vsftpd实现用户不同权限:只能下载,可上传,管理权限等 [仅供参考未亲测]
- java 自定义异常,记录日志简单说明!留着以后真接复制
- Ubuntu14 中安装 VMware10 Tools工具<2>
- android示例之遍历SD卡的文件并显示
- 1Z0-051 QUESTION 24 数据类型应该注意的地方
- 实现TabHost的两种方法
- 针对百度近期频繁调整算法的一些看法
- 浅谈对病毒式营销认识
- css position,display 学习
- Java 控制音量
- 关于ArcGIS Engine中获取权限的问题
- 网站优化SEO如何稳定
- SEO优化之提高优化的效率的四个技巧
- mysql注入初体验