ANDROID_MARS学习笔记_S01原始版_022_MP3PLAYER002_本地及remote标签

一、简介

1.在main.xml中用TabHost、TabWidget、FrameLayout标签作布局

2.在MainActivity中生成TabHost、TabSpec,调用setIndicator()、setContent()、addTab(),用Intent指明要跳转的tab对应的class

3.在onResume中写获取本地mp3信息的代码,代码在onResume中,则切换tab时,mp3信息每次都会更新

二、代码
1.xml
(1)main.xml

 1 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:id="@android:id/tabhost" android:layout_width="fill_parent"
 3     android:layout_height="fill_parent">
 4     <LinearLayout android:orientation="vertical"
 5         android:layout_width="fill_parent" android:layout_height="fill_parent"
 6         android:padding="5dp">
 7         <TabWidget android:id="@android:id/tabs"
 8             android:layout_width="fill_parent" android:layout_height="wrap_content" />
 9         <FrameLayout android:id="@android:id/tabcontent"
10             android:layout_width="fill_parent" android:layout_height="fill_parent"
11             android:padding="5dp" />
12     </LinearLayout>
13 </TabHost> 

(2)AndroidManifest.xml

增加activity

1 <activity android:name=".LocalMp3ListActivity" android:label="@string/app_name"/>
2 <activity android:name=".Mp3ListActivity" android:label="@string/app_name"/>

2.java
(1)MainActivity.java

 1 package tony.mp3player;
 2 
 3 import android.app.TabActivity;
 4 import android.content.Intent;
 5 import android.content.res.Resources;
 6 import android.os.Bundle;
 7 import android.widget.TabHost;
 8 
 9 public class MainActivity extends TabActivity {
10 
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.main);
15         //得到TabHost对象,对TabActivity的操作通常都有这个对象完成
16         TabHost tabHost = getTabHost();
17         //生成一个Intent对象,该对象指向一个Activity
18         Intent remoteIntent = new Intent();
19         remoteIntent.setClass(this, Mp3ListActivity.class);
20         //生成一个TabSpec对象,这个对象代表了一个页
21         TabHost.TabSpec remoteSpec = tabHost.newTabSpec("Remote");
22         Resources res = getResources();
23         //设置该页的indicator
24         remoteSpec.setIndicator("Remote", res.getDrawable(android.R.drawable.stat_sys_download));
25         //设置该页的内容
26         remoteSpec.setContent(remoteIntent);
27         //将设置好的TabSpec对象添加到TabHost当中
28         tabHost.addTab(remoteSpec);
29         
30         Intent localIntent = new Intent();
31         localIntent.setClass(this, LocalMp3ListActivity.class);
32         TabHost.TabSpec localSpec = tabHost.newTabSpec("Local");
33         localSpec.setIndicator("Local", res.getDrawable(android.R.drawable.stat_sys_upload));
34         localSpec.setContent(localIntent);
35         tabHost.addTab(localSpec);
36     }
37 }

(2)FileUtils.java增加函数

 1     public List<Mp3Info> getMp3Infos(String path) {
 2         List<Mp3Info> list = new ArrayList<Mp3Info>();
 3         File file = new File(SDCardRoot + File.separator + path);
 4         File[] files = file.listFiles();
 5         for(File tempFile : files) {
 6             if(tempFile.getName().endsWith(".mp3")) {
 7                 list.add(new Mp3Info(tempFile.getName(), tempFile.length()+""));
 8             }
 9         }
10         return list;
11     }

 

原文地址:https://www.cnblogs.com/shamgod/p/5196228.html