Android基於API的Tabs3實現仿優酷tabhost效果執行個體_Android

來源:互聯網
上載者:User

本文執行個體講述了Android基於API的Tabs3實現仿優酷tabhost效果。分享給大家供大家參考,具體如下:

前兩天老師就讓自己寫個視頻播放器用戶端,這個是他上課講的一個小小demo,通過查看安卓API的tabs3,實現仿優酷視頻用戶端的tabhost效果。我的API路徑是D:\android\sdk\samples\android-17\ApiDemos\src\com\example\android\apis\view下的Tabs3,下面是實現效果:

廢話不多說了,直接上碼:

MainActivity.java

package com.example.video;import android.os.Bundle;import android.R.layout;import android.app.Activity;import android.app.TabActivity;import android.content.Intent;import android.view.LayoutInflater;import android.view.Menu;import android.widget.TabHost;public class MainActivity extends TabActivity {  public TabHost tabHost;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    //擷取對象    tabHost = getTabHost();    tabHost.addTab(tabHost.newTabSpec("myself")        .setIndicator("個人中心")        .setContent(new Intent(this, MySelfActivity.class)));    tabHost.addTab(tabHost.newTabSpec("myindex")        .setIndicator("優酷首頁")        .setContent(new Intent(this, MyIndexActivity.class)));    tabHost.addTab(tabHost.newTabSpec("mycenter")        .setIndicator("頻道中心")        .setContent(new Intent(this, MyCenterActivity.class)));    //指定的當前的tab    //通過索引指定 索引從0開始    tabHost.setCurrentTab(0); //從零開始    //通過標識來啟用   // tabHost.setCurrentTabByTag("XXX1");  }  @Override  public boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.    getMenuInflater().inflate(R.menu.main, menu);    return true;  }}

MyCenterActivity.java

package com.example.video;import android.app.Activity;import android.os.Bundle;public class MyCenterActivity extends Activity{  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_mycenter);  }}

MyIndexActivity.java

package com.example.video;import android.app.Activity;import android.os.Bundle;public class MyIndexActivity extends Activity{  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_myindex);  }}

MySelfActivity.java

package com.example.video;import android.app.Activity;import android.os.Bundle;public class MySelfActivity extends Activity{  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_myself);  }}

下面是布局檔案:

activity_mycenter.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"  android:paddingBottom="@dimen/activity_vertical_margin"  android:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  tools:context=".MainActivity" >  <TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="優酷中心" /></RelativeLayout>

activity_myindex.xml

<TextView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="優酷首頁" />

activity_myself.xml

<TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="個人首頁" />

當然別忘了在資訊清單檔中配置activity

<!-- 配置activity組件 --><activity android:name="com.example.video.MyIndexActivity"/><activity android:name="com.example.video.MySelfActivity"/><activity android:name="com.example.video.MyCenterActivity"/>

希望本文所述對大家Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.