声明:本人博客纯属个人学习过程中的一些仿写的简单练习记录,其他论坛也有类似内容!(可能不免有错误之处,还望见谅,指出)

这里是Android基本组件Button和Tab_Widget的简单应用,通过点击Tab1、Tab2、Tab3、来切换查看内容

图为加入TabView按钮的Activity和点击Tab切换效果演示
Android 基本组件 Button和Tab_WidgetAndroid 基本组件 Button和Tab_WidgetAndroid 基本组件 Button和Tab_Widget

项目名为: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:andro
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: />
</LinearLayout>
TabDemoActivity 的视图布局文件 tab_demo xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:andro
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:
        android:background="@drawable/one"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/tab1"/>
    <TextView android:
        android:background="@drawable/three"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/tab2"/>
    <TextView android:
        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:andro
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> 

相关文章:

  • 2021-04-01
  • 2021-11-14
  • 2021-05-31
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-04-21
  • 2022-12-23
  • 2021-04-11
  • 2022-12-23
相关资源
相似解决方案