android账本-主界面(切换卡)

布局文件

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <RelativeLayout 
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
       
        <FrameLayout 
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
        	android:layout_height="match_parent">
            
            <TextView 
                android:id="@+id/tv1"
                android:layout_width="match_parent"
        		android:layout_height="match_parent"/>
            
            <TextView 
                android:id="@+id/tv2"
                android:layout_width="match_parent"
        		android:layout_height="match_parent"/>
            
            <TextView 
                android:id="@+id/tv3"
                android:layout_width="match_parent"
        		android:layout_height="match_parent"/>
            
        </FrameLayout>
        
        <TabWidget 
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
        	android:layout_height="wrap_content"
        	android:layout_alignParentBottom="true"/>
        
    </RelativeLayout>

</TabHost>

MainActivity.java

package com.example.account;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;


@SuppressWarnings({ "deprecation" })
public class MainActivity extends TabActivity {

	TabHost tabHost;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        tabHost = getTabHost();
           
        //newTabSpec设置标签,跟下面响应时间对应。
        //setIndicator设置页面标题。
        tabHost.addTab(tabHost.newTabSpec("0").setIndicator("主页").setContent(R.id.tv1));
        tabHost.addTab(tabHost.newTabSpec("1").setIndicator("流水").setContent(R.id.tv2));
        tabHost.addTab(tabHost.newTabSpec("2").setIndicator("总账").setContent(R.id.tv3));
        
        //设置背景颜色
        //tabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
        
        //设置默认界面
        tabHost.setCurrentTab(0);
        
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			
			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				if(tabId == "0"){
					Toast.makeText(MainActivity.this, "tab1", Toast.LENGTH_LONG).show();
				}else if(tabId == "1"){
					Toast.makeText(MainActivity.this, "tab2", Toast.LENGTH_LONG).show();
				}else if(tabId == "2"){
					Toast.makeText(MainActivity.this, "tab3", Toast.LENGTH_LONG).show();
				}
			}
		});
    }


    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


效果图:


遇到的问题:

1.布局

(1)TabHost的android:id必须设置为:android:id="@android:id/tabhost"

(2)TabWidget的android:id必须设置为:android:id="@android:id/tabs"

(3)FrameLayout的android:id必须设置为:android:id="@android:id/tabcontent"

2.监听事件

监听事件里的tabId == "0",这里的值必须跟tabHost.newTabSpec("0")里设置的相对应。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值