Android開發之自訂TabHost文字及背景(原始碼分享)

來源:互聯網
上載者:User

標籤:android   tabhost   tab   relativelayout   原始碼   

    使用TabHost 可以在一個螢幕間進行不同版面的切換,而系統內建的tabhost介面較為樸素,我們應該如何進行自訂修改最佳化呢

MainActivity的原始碼

package com.dream.ledong;import android.app.TabActivity;import android.content.Intent;import android.graphics.Color;import android.os.Bundle;import android.view.Gravity;import android.widget.RelativeLayout;import android.widget.TabHost;import android.widget.TabHost.OnTabChangeListener;import android.widget.TabWidget;import android.widget.TextView;import com.example.client.R;@SuppressWarnings("deprecation")public class itemList extends TabActivity {@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.itemlist);final TabHost tabHost = getTabHost();Intent remoteIntent = new Intent(itemList.this, item1.class);TabHost.TabSpec remoteTabSpec = tabHost.newTabSpec("remote");remoteTabSpec.setIndicator("運動推薦");remoteTabSpec.setContent(remoteIntent);tabHost.addTab(remoteTabSpec);Intent localIntent = new Intent(itemList.this, item2.class);TabHost.TabSpec localTabSpec = tabHost.newTabSpec("local");localTabSpec.setIndicator("球友人氣");localTabSpec.setContent(localIntent);tabHost.addTab(localTabSpec);Intent localIntent2 = new Intent(itemList.this, item2.class);TabHost.TabSpec localTabSpec2 = tabHost.newTabSpec("a");localTabSpec2.setIndicator("競技氛圍");localTabSpec2.setContent(localIntent2);tabHost.addTab(localTabSpec2);        updateTabStyle(tabHost);// 當某個Tab被選中時,則更新背景樣式tabHost.setOnTabChangedListener(new OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {updateTabStyle(tabHost);}});}private void updateTabStyle(final TabHost mTabHost) {TabWidget tabWidget = mTabHost.getTabWidget();tabWidget.setRightStripDrawable(R.drawable.list_item_divide_operate);tabWidget.setLeftStripDrawable(R.drawable.list_item_divide_operate);for (int i = 0; i < tabWidget.getChildCount(); i++) {RelativeLayout tabView = (RelativeLayout) mTabHost.getTabWidget().getChildAt(i);TextView text = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);text.setTextSize(15);RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) text.getLayoutParams();params.width = RelativeLayout.LayoutParams.MATCH_PARENT;params.height = RelativeLayout.LayoutParams.MATCH_PARENT;text.setLayoutParams(params); text.setGravity(Gravity.CENTER);if (mTabHost.getCurrentTab() == i) {// 選中tabView.setBackgroundColor(Color.parseColor("#8DB6CD"));text.setTextColor(this.getResources().getColorStateList(android.R.color.black));} else {// 未選中tabView.setBackgroundColor(Color.parseColor("#ffffff"));text.setTextColor(this.getResources().getColorStateList(android.R.color.darker_gray));}}}}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.