android 底部菜单栏(仿微信顶部菜单栏)
虾米姐
阅读:670
2021-04-01 10:13:48
评论:0
效果截图:
布局文件:
tabhost.xml
<?xml version="1.0" encoding="UTF-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/tab_bkg"
android:fadingEdge="none"
android:fadingEdgeLength="0.0px" />
</RelativeLayout>
</TabHost>
核心代码:
package com.rf.main;
import com.rf.function.AttentionActivity;
import com.rf.function.HomeTimeLineActivity;
import com.rf.function.MessageActivity;
import com.rf.function.ReferActivity;
import com.rf.function.SearchActivity;
import com.rf.qqweixun.R;
import android.app.TabActivity;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
public class MainActivity extends TabActivity {
// 相关资源
private TabHost tabHost;
private RadioGroup mainbtGroup;
private static final String HOME = "主页";
private static final String REFER = "提及";
private static final String SECRET = "私信";
private static final String SEARCH = "搜索";
private static final String ATTENTIION = "关注";
@Override
public void setContentView(int layoutResID) {
// TODO 全屏显示代码
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.setContentView(R.layout.tabhost);
tabHost = this.getTabHost();
View view1 = View.inflate(MainActivity.this, R.layout.tab, null);
((ImageView) view1.findViewById(R.id.tab_imageview_icon)).setImageResource(R.drawable.tab_timeline_selector);//设置每一个tab的图标
((TextView) view1.findViewById(R.id.tab_textview_title)).setText(HOME);
TabHost.TabSpec spec1 = tabHost.newTabSpec(HOME)
.setIndicator(view1)
.setContent(new Intent(this, HomeTimeLineActivity.class));
tabHost.addTab(spec1);
View view2 = View.inflate(MainActivity.this, R.layout.tab, null);
((ImageView) view2.findViewById(R.id.tab_imageview_icon)).setImageResource(R.drawable.tab_atme_selector);
((TextView) view2.findViewById(R.id.tab_textview_title)).setText(REFER);
TabHost.TabSpec spec2 = tabHost.newTabSpec(REFER)
.setIndicator(view2)
.setContent(new Intent(this, ReferActivity.class));
tabHost.addTab(spec2);
View view3 = View.inflate(MainActivity.this, R.layout.tab, null);
((ImageView) view3.findViewById(R.id.tab_imageview_icon)).setImageResource(R.drawable.tab_message_selector);
((TextView) view3.findViewById(R.id.tab_textview_title)).setText(SECRET);
TabHost.TabSpec spec3 = tabHost.newTabSpec(SECRET)
.setIndicator(view3)
.setContent(new Intent(this, MessageActivity.class));
tabHost.addTab(spec3);
View view4 = View.inflate(MainActivity.this, R.layout.tab, null);
((ImageView) view4.findViewById(R.id.tab_imageview_icon)).setImageResource(R.drawable.tab_explore_selector);
((TextView) view4.findViewById(R.id.tab_textview_title)).setText(SEARCH);
TabHost.TabSpec spec4 = tabHost.newTabSpec(SEARCH)
.setIndicator(view4)
.setContent(new Intent(this, SearchActivity.class));
tabHost.addTab(spec4);
View view5 = View.inflate(MainActivity.this, R.layout.tab, null);
((ImageView) view5.findViewById(R.id.tab_imageview_icon)).setImageResource(R.drawable.tab_focus_selector);
((TextView) view5.findViewById(R.id.tab_textview_title)).setText(ATTENTIION);
TabHost.TabSpec spec5 = tabHost.newTabSpec(ATTENTIION)
.setIndicator(view5)
.setContent(new Intent(this, AttentionActivity.class));
tabHost.addTab(spec5);
}
}
样式布局文件:tab.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/tab_imageview_icon"
android:layout_width="fill_parent"
android:layout_height="32.0dip"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/tab_textview_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:gravity="center"
android:marqueeRepeatLimit="1"
android:singleLine="true"
android:textSize="11.0sp" />
</LinearLayout>
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。