解析Android Widget組件應用學習
Android Widget組件應用學習是本文要介紹的內容,主要是來了解并學習Android Widget組件應用,具體內容的學習來看本文詳解。
這里是Android基本組件Button和Tab_Widget的簡單應用,通過點擊Tab1、Tab2、Tab3、來切換查看內容
圖為加入TabView按鈕的Activity和點擊Tab切換效果演示
項目名為:Tab
代碼如下:
- package com.tab.demo;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class MainActivity extends Activity {
- private Button button; //構建按鈕對象
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button=(Button)findViewById(R.id.tab_demo_button); //實例化對象,聯系到布局文件
- button.setOnClickListener(new Button.OnClickListener() {
- //設置監聽器,監聽點擊事件
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent = new Intent();
- intent.setClass(MainActivity.this, TabDemoActivity.class);
- startActivity(intent); //實現點擊跳轉
- }
- });
- }
- }
- //跳轉到TabDemoActivity的代碼
- package com.tab.demo;
- import android.app.TabActivity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.widget.TabHost;
- public class TabDemoActivity extends TabActivity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- TabHost tabHost = getTabHost(); //構造一個Tab標簽容器TabHost
- LayoutInflater.from(this).inflate(R.layout.tab_demo,
- tabHost.getTabContentView(), true);
- tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1")
- .setContent(R.id.view1)); //分別把構造好的標簽放入TabHost里面
- tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2")
- .setContent(R.id.view2));
- tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Tab3")
- .setContent(R.id.view3));
- }
- }
MainActivity的視圖布局文件 main xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="@string/hello" />
- <Button android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="TabView"
- android:id="@+id/tab_demo_button" />
- </LinearLayout>
TabDemoActivity 的視圖布局文件 tab_demo xml:
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView android:id="@+id/view1"
- android:background="@drawable/one"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="@string/tab1"/>
- <TextView android:id="@+id/view2"
- android:background="@drawable/three"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="@string/tab2"/>
- <TextView android:id="@+id/view3"
- android:background="@drawable/two"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="@string/tab3"/>
- </FrameLayout>
相關信息文件string xml :
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, Tab!</string>
- <string name="app_name">Tab</string>
- <string name= "tab1">Here is tab1.</string>
- <string name= "tab2">Jump to Tab2.</string>
- <string name= "tab3">The end is Tab3.</string>
- </resources>
應用配置文件 AndroidMainfest xml :
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.tab.demo" android:versionCode="1" android:versionName="1.0">
- <application android:icon="@drawable/me" android:label="@string/app_name">
- <activity android:name=".MainActivity" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".TabDemoActivity"></activity>
- </application>
- </manifest>
小結:解析Android Widget組件應用學習的內容介紹完了,希望通過Android組件Tab_Widget內容的學習能對你有所幫助!