Android Fragment+FragmentTabHost組件實現常見首頁面(仿微信新浪)_Android

來源:互聯網
上載者:User

採取的方法是Fragment+FragmentTabHost組件來實現這種常見的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[] = {"首頁", "圈子", "資訊","個人中心"};   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); }   /**   * 項的樣式   * @param index 第幾個   * @return 每一個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;   }     }

通過以上文章,希望能協助到大家,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.