在一个页面上会出现几个不同的菜单项(卡片),点击菜单项,就会进入不同的页面。这样的功能在安卓应用程序中经常出现,下面我们来分享一下它的使用方法。
这个例子实现的是在页面的三个卡片上点击任意一个卡片,进入不同的页面。
整体思路:在xml文件中使用FrameLayout布局方式,在里面添加2个LinearLayout布局和1个TextView控件,宽度和高度都是填充父窗口;在活动中定义一个TabHost类对象,把这个TabHost类对象和布局文件绑定到这个活动上,把2个LinearLayout布局和1个TextView控件添加到TabHost类对象中,设置setOnTabChangedListener事件,在里面根据选择的不同卡片使用Toast方式显示不同的信息。
activity_main.xml文件:
复制代码
MainActivity.java文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39<!-- 第一个布局 --> <LinearLayout android:id="@+id/view1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="罗志祥" /> </LinearLayout> <!-- 第二布局 --> <LinearLayout android:id="@+id/view2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="周杰伦" /> </LinearLayout> <!-- 第三布局 --> <TextView android:id="@+id/view3" android:background="#00ff00" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="陈奕迅" />
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31public class MainActivity extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle("TabDemoActivity"); TabHost tabHost=getTabHost(); LayoutInflater.from(this).inflate(R.layout.activity_main, tabHost.getTabContentView(),true); tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.view1)); tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2").setContent(R.id.view2)); tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3").setContent(R.id.view3)); //当选择的tab发生变化的时候触发的事件 tabHost.setOnTabChangedListener(new OnTabChangeListener() { @Override public void onTabChanged(String tabId) { if(tabId.equals("tab1")){ Toast.makeText(MainActivity.this, "显示tab1", 1).show(); } else if(tabId.equals("tab2")){ Toast.makeText(MainActivity.this, "显示tab2", 1).show(); } else if(tabId.equals("tab3")){ Toast.makeText(MainActivity.this, "显示tab3", 1).show(); } } }); } }
最后
以上就是义气月饼最近收集整理的关于安卓控件使用系列28:TabHost卡片的使用方法1继承TabActivity的全部内容,更多相关安卓控件使用系列28:TabHost卡片内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复