這是中國大學慕課移動終端應用開發的網課作業6
效果圖
代碼
主要佈局 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/mi">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:layout_alignParentBottom="true"
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="540dp">
<LinearLayout
android:id="@+id/layout_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
android:orientation="vertical"
android:paddingTop="20dp"
>
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="主頁"
android:layout_centerInParent="true"
android:textSize="30dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layout_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/yellow"
android:orientation="vertical"
android:paddingTop="20dp">
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分類"
android:textSize="30dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layout_cart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green"
android:orientation="vertical"
android:paddingTop="20dp">
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="購物車"
android:textSize="30dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layout_nickname"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue"
android:orientation="vertical"
android:paddingTop="20dp"
>
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的"
android:textSize="30dp" />
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>
主邏輯代碼 MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private TabHost mTabHost;
private void init(){
mTabHost = findViewById(R.id.tabHost);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
//啓動選項卡之前必須調用此方法
mTabHost.setup();
//想要加上圖片,必須先新建一個tab icon佈局文件,然後調用getTabView()方法,在底下,自己寫的,返回一個view
/**
* newTabSpec("tabRed") 設置此選項卡的標記,單機事件觸發後,String tabId就是這個
*
* .setIndicator(getTabView(R.drawable.home,"紅色")) 設置選項卡張啥樣
*
* .setContent(R.id.layout_red); 設置選項卡內容是哪一個layout
*/
TabHost.TabSpec tsHome = mTabHost.newTabSpec("tabHome")
.setIndicator(getTabView(R.drawable.home,"主頁"))
.setContent(R.id.layout_home);
mTabHost.addTab(tsHome);
TabHost.TabSpec tsMenu = mTabHost.newTabSpec("tabMenu")
.setIndicator(getTabView(R.drawable.menu,"分類"))
.setContent(R.id.layout_menu);
mTabHost.addTab(tsMenu);
TabHost.TabSpec tsCart = mTabHost.newTabSpec("tabCart")
.setIndicator(getTabView(R.drawable.cart,"購物車"))
.setContent(R.id.layout_cart);
mTabHost.addTab(tsCart);
TabHost.TabSpec tsNickname = mTabHost.newTabSpec("tabNickname")
.setIndicator(getTabView(R.drawable.nickname,"我的"))
.setContent(R.id.layout_nickname);
mTabHost.addTab(tsNickname);
//給選項卡增加監聽事件
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
Toast.makeText(MainActivity.this,tabId,Toast.LENGTH_SHORT).show();
}
});
//設置默認是哪個選項卡
mTabHost.setCurrentTab(0);
}
//獲取選項卡的樣式,返回一個view 對象
public View getTabView(int imgId,String text){
LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.tabicon,null);
ImageView imageView = linearLayout.findViewById(R.id.img);
imageView.setBackgroundResource(imgId);
TextView textView = linearLayout.findViewById(R.id.myText);
textView.setText(text);
return linearLayout;
}
}
選項卡布局 tabicon.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_gravity="center"
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:layout_gravity="center"
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"/>
</LinearLayout>
資源文件 colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="red">#F5BCBB</color>
<color name="yellow">#FCE4A6</color>
<color name="green">#BEEEC0</color>
<color name="blue">#AEDFF1</color>
<color name="mi">#F8F5D6</color>
</resources>
圖片資源
所有的圖片資源均來自阿里巴巴矢量圖標庫
以下圖片分別爲:home.png,menu.png,cart.png,nickname.png