Android Fragment+FragmentTabHost組件實(shí)現(xiàn)常見主頁面(仿微信新浪)
采取的方法是Fragment+FragmentTabHost組件來實(shí)現(xiàn)這種常見的app主頁面的效果
首先給出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="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="@color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/color_home_tab_line" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/et_divider_disable">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout> </LinearLayout>
主代碼:
public class MainActivity
{ @ViewInject(android.R.id.tabhost)
private FragmentTabHost mTabHost;
private LayoutInflater layoutInflater;
private int mImageViewArray[] = {R.drawable.home_tab1, R.drawable.home_tab2, R.drawable.home_tab3, R.drawable.home_tab4};
private String mTextviewArray[] = {"首頁", "圈子", "資訊","個(gè)人中心"};
private Class fragmentArray[] = {Fragment1.class, Fragment2.class, Fragment3.class,Fragment4.class};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
@Override
protected void init() {
// list=new JSONArray();
layoutInflater=LayoutInflater.from(this);
initTabHost();//初始化底部菜單
}
/**
* 初始化底部工具欄
*/
private void initTabHost() {
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
int count = fragmentArray.length;
for (int i = 0; i < count; i++) {
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
.setIndicator(getTabItemView(i));
mTabHost.addTab(tabSpec, fragmentArray[i], null);
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.color.white);
}
mTabHost.setCurrentTabByTag(mTextviewArray[0]);
mTabHost.getTabWidget().setDividerDrawable(null);
}
/**
* 項(xiàng)的樣式
* @param index 第幾個(gè)
* @return 每一個(gè)Tab樣式
*/
private View getTabItemView(int index) {
View view = layoutInflater.inflate(R.layout.tab_home_item, null);
ImageView imageView = (ImageView) view.findViewById(R.id.icon);
imageView.setImageResource(mImageViewArray[index]);
TextView textView = (TextView) view.findViewById(R.id.name);
textView.setText(mTextviewArray[index]);
return view;
}
}
通過以上文章,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android清空編輯框內(nèi)容功能的實(shí)現(xiàn)實(shí)例代碼
本篇文章主要介紹了Android清空編輯框數(shù)據(jù)功能的實(shí)現(xiàn)實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-03-03
利用Android中的TextView實(shí)現(xiàn)逐字顯示動畫
在安卓程序啟動的時(shí)候,想逐字顯示一段話,每個(gè)字都有一個(gè)從透明到不透明的漸變動畫。那如何顯示這個(gè)效果,下面一起來看看。2016-08-08
Android數(shù)據(jù)流之Channel和Flow實(shí)現(xiàn)原理和技巧詳解
在 Android 應(yīng)用程序的開發(fā)中,處理異步數(shù)據(jù)流是一個(gè)常見的需求,為了更好地應(yīng)對這些需求,Kotlin 協(xié)程引入了 Channel 和 Flow,它們提供了強(qiáng)大的工具來處理數(shù)據(jù)流,本文將深入探討 Channel 和 Flow 的內(nèi)部實(shí)現(xiàn)原理、高級使用技巧以及如何在 Android 開發(fā)中充分利用它們2023-11-11
詳解如何使用Android Studio 進(jìn)行NDK開發(fā)和調(diào)試
本篇文章主要介紹了詳解如何使用Android Studio 進(jìn)行NDK開發(fā)和調(diào)試,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
Android開發(fā)之獲取LayoutInflater對象的方法總結(jié)
這篇文章主要介紹了Android開發(fā)之獲取LayoutInflater對象的方法,結(jié)合實(shí)例形式總結(jié)分析了Android獲取LayoutInflater對象的常用技巧,需要的朋友可以參考下2016-02-02
React Native開發(fā)中自動打包腳本的實(shí)例代碼
這篇文章主要介紹了React Native開發(fā)中自動打包腳本的實(shí)例代碼,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
詳解android特性之CoordinatorLayout用法探析實(shí)例
本篇文章主要介紹了android特性之CoordinatorLayout用法探析實(shí)例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02

