• 仿Mars MP3播放器项目2


      本节将介绍主界面的UI设计以及代码实现

      用到的一个关键是TabActivity

      这里需要实现的是一个服务器端文件的显示以及本地的文件显示,而这2个功能的实现分别是2个Activity,因此使用到TabActivity

      先来主界面的UI吧,main.xml:

      

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

    这里的xml布局文件不在使用我们熟悉的5种Layout,而是使用TabHost。其中tabHost,TabWidget,FrameLayout的ID值都是默认的android:id,这是要注意的,当然你可以进行自定义。

      下面是运行MP3播放器的主界面的MainActivity.java:

     1 import *; //这里是各类包的引用
    2 public class MainActivity extends ActivityGroup{
    3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 // TODO Auto-generated method stub 6 super.onCreate(savedInstanceState); 7 //layout引入的是main.xml
    8   setContentView(R.layout.main);

    9 //得到tabHost对象 10 TabHost tabHost = (TabHost)findViewById(R.id.tabhost); 11 tabHost.setup(); 12 tabHost.setup(this.getLocalActivityManager()); 13 //生成一个Intent对象,该对象指向一个Activity 14 Intent remoteIntent = new Intent(); 15 remoteIntent.setClass(this, Mp3ListActivity.class); 16 //生成一个TabSpec对象,这个对象代表一个类 17 TabHost.TabSpec remoteSpec = tabHost.newTabSpec("Remote"); 18 Resources res = getResources(); 19 //设置该页的Indicator 20 remoteSpec.setIndicator("Remote",res.getDrawable(android.R.drawable.stat_sys_download)); 21 //设置该页的内容 22 remoteSpec.setContent(remoteIntent); 23 //将设置好的TabSpec对象TabHost中 24 tabHost.addTab(remoteSpec); 25 Intent localIntent = new Intent(); 26 localIntent.setClass(this, LocalMp3ListActivity.class); 27 TabHost.TabSpec localSpec = tabHost.newTabSpec("Local"); 28 localSpec.setIndicator("Local", res.getDrawable(android.R.drawable.stat_sys_upload)); 29 localSpec.setContent(localIntent); 30 tabHost.addTab(localSpec); 31 } 32 }

      大概的注释都在代码中,其中要注意的是:TabActivity在Android 4.0之后就被google给弃用了,也就是现在使用的话会在TabActivity中间出现横杠,需要让你添加@SuppressWarnings,我上网查找了相关资料,选择了用ActivityGroup进行继承,虽然也是显示depercated,但是好在可以运行。官方的建议是使用Fragment进行替代。

      a.如果是TabActivity的话,要获取TabHost对象直接使用  TabHost tabHost = getTabHost();

      这里使用ActivityGroup,获取TabHost对象是使用

              TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
              tabHost.setup();

              tabHost.setup(this.getLocalActivityManager());

      b.生成一个TabSpec对象,一个该对象代表一个类;通过setIndicator来设置该页的Indicator,Mars这里直接使用的是Android自带的resources,我们也可以自定义;

      c.将设置好的TabSpec对象add到TabHost中;

      d.这里和Mars的一样,暂时为2个Activity,一个是本地LocalMP3ListActivity,另一个是服务器端MP3ListActivity;点击不同的TabSpec会进行跳转到不同的Activity中

    附上一张效果图:

      

      下次介绍的是如何从服务器端解析resources.xml并且通过ListView显示在Remote中

    附:TabActivity的学习链接:

      http://www.cnblogs.com/keyindex/articles/1815074.html

      http://developer.android.com/reference/android/widget/TabHost.html  

      http://blog.csdn.net/lastsweetop/article/details/5566200

      

      

      

      

      

  • 相关阅读:
    leetcode(5)-罗马数字转整数
    leetcode(4)-整数反转
    leetcode(3)-回文数
    leetcode(2)-有效的括号
    leetcode(1)-两数之和
    HTTP基础(一)
    ubuntu 18.04安装MariaDB 10.04并通过远程navicat连接
    ubuntu18.04 root用户登录
    xshell连接ubuntu虚拟机
    ubuntu18.04使用node压缩包的安装及配置
  • 原文地址:https://www.cnblogs.com/Rose127/p/3211178.html
Copyright © 2020-2023  润新知