Android开发中项目实现一个自定义Tab选项卡功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。具体如下:import android.app.TabActivity;
Android开发中项目实现一个自定义Tab选项卡功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
具体如下:
import android.app.TabActivity;import android.content.Intent;import android.os.Bundle;import android.widget.*;import android.widget.TabHost.OnTabChangeListener;import android.os.Build;import android.view.View;import java.lang.reflect.Field;import android.view.LayoutInflater;public class testTabActivity extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int width =45; int height =48; final TabHost tabs = getTabHost(); final TabWidget tabWidget = tabs.getTabWidget(); Field mBottomLeftStrip; Field mBottomRightStrip; LayoutInflater.from(this).inflate(R.layout.tab_views, tabs.getTabContentView(), true); tabs.addTab(tabs.newTabSpec("first tab") .setIndicator("信息",getResources().getDrawable(R.drawable.m)) .setContent(new Intent(testTabActivity.this,OneActivty.class)) ); tabs.addTab(tabs.newTabSpec("second tab") .setIndicator("收藏",getResources().getDrawable(R.drawable.n)) .setContent(R.id.content)); tabs.addTab(tabs.newTabSpec("second tab") .setIndicator("设置",getResources().getDrawable(R.drawable.s)) .setContent(R.id.content)); for (int i =0; i < tabWidget.getChildCount(); i++) { tabWidget.getChildAt(i).getLayoutParams().height = height; tabWidget.getChildAt(i).getLayoutParams().width = width; final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title); tv.setTextColor(this.getResources().getColorStateList(android.R.color.white)); if (Float.valueOf(Build.VERSioN.RELEASE) <= 2.1) { try { mBottomLeftStrip = tabWidget.getClass().getDeclaredField ("mBottomLeftStrip"); mBottomRightStrip = tabWidget.getClass().getDeclaredField ("mBottomRightStrip"); if(!mBottomLeftStrip.isAccessible()) { mBottomLeftStrip.setAccessible(true); } if(!mBottomRightStrip.isAccessible()){ mBottomRightStrip.setAccessible(true); } mBottomLeftStrip.set(tabWidget, getResources().getDrawable (R.drawable.no)); mBottomRightStrip.set(tabWidget, getResources().getDrawable (R.drawable.no)); } catch (Exception e) { e.printStackTrace(); } } else { } View vvv = tabWidget.getChildAt(i); if(tabs.getCurrentTab()==i){ vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_button)); } else { vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bar)); } } tabs.setOnTabChangedListener(new OnTabChangeListener(){ @Override public void onTabChanged(String tabId) { // TODO Auto-generated method stub for (int i =0; i < tabWidget.getChildCount(); i++) { View vvv = tabWidget.getChildAt(i); if(tabs.getCurrentTab()==i){ vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_button)); } else { vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bar)); } } }}); }}
--结束END--
本文标题: Android开发中项目实现一个自定义Tab选项卡功能
本文链接: https://www.lsjlt.com/news/225672.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0