学生要做一个滴滴矿车的项目,遇到一个bug不好解决。看代码的耐心都没有了,我给他建议先把ui框架搞通。顺手说下,他学的在我们班基本垫底了。但是很顽强,拿了四个offer,原先拿了两个,上班10天,感觉老板不顺眼(学生确实也比较任性),后来自己出来,有面试两家,均拿到offer,一个12k没去,去了这家。滴滴矿车。
看到项目后我就说让他先搞懂整体的ui架子,写一个demo(他这么做了,感觉效果很好),然后在看代码,顺手自己也写了一个,实在是很久没用过这玩意儿了。如下:
类的结构:
布局文件也很简单:
mainacitivity
package com.example.alan.mytabactivity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class MainActivity extends TabActivity {
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 拿到tabhost对象
TabHost tabHost = getTabHost();
// 创建我们需要用的变量,两个。tabspec,这个tabhost上面的东西。
TabHost.TabSpec spec;
intent = new Intent().setClass(this, ArtistsActivity.class);
//设置tabhost的指针。绑定第一个跳转的intent,加入这个tab页。
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
getResources().getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// 同样的道理绑定第二个。
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
getResources().getDrawable(R.drawable.btn_canvas_1_b))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
getResources().getDrawable(R.drawable.btn_canvas_1_c))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
主类对应的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="com.example.alan.MainActivity">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
</android.support.design.widget.CoordinatorLayout>
seelector文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/btn_canvas_1_b"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/btn_canvas_1_c" />
</selector>