运行效果:
image.png
运行环境:
AndroidStdio
1.创建一个类继承于ViewGroup管理每一个控件的布局
public class TabBarView extends ViewGroup {
//保存所有标签的模型数据
private ArrayList items;
public TabBarView(Context context) {
super(context);
}
public TabBarView(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (items != null) {
//每个tab的尺寸
float density = getResources().
getDisplayMetrics().density;
int width = (int) (70 * density);
//计算间距
int padding = (getWidth()-width*items.size()) /
(items.size()+1);
for (int i = 0; i < items.size(); i++){
//获取图片id对应的Drawable对象
TabModel model = items.get(i);
//创建一个标签视图
TabBotton v = new TabBotton(getContext(),model.icon_normal,model.icon_select,model.title);
//具体布局
int left = (i+1)*padding + i * width;
int top = (int) (5*density);
int right = left + width;
int bottom = (int) (getHeight()-5*density);
v.layout(left,top,right,bottom);
//添加
addView(v);
}
}
}
//提供一个内部类
//用来管理数据模型
public static class TabModel{
public int icon_normal;
public int icon_select;
public String title;
public TabModel(int icon_normal, int icon_select, String title) {
this.icon_normal = icon_normal;
this.icon_select = icon_select;
this.title = title;
}
}
public void setItems(ArrayList items) {
this.items = items;
}
}
2.在xml中配置
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/root">
android:id="@+id/tab_bar"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:background="@color/colorAccent"
>
3.创建一个类管理控件的具体内容
public class TabBotton extends AppCompatButton {
private int normal;
private int select;
private String title;
//记录一下标签传递的内容
public TabBotton(Context context) {
super(context);
}
public TabBotton(Context context,int normal,int select,String title) {
this(context);
this.normal = normal;
this.select = select;
this.title = title;
float density = getResources().getDisplayMetrics().density;
//获取id对应的drawable对象
Drawable drawable = getResources().getDrawable(normal);
//设置图片的尺寸
drawable.setBounds(0,0,(int)(32*density),(int)(32*density));
//设置正常状态的图片和文字
setCompoundDrawables(null,drawable,null,null
);
setText(title);
}
public TabBotton(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
4.在主界面里面设置具体的属性、内容值
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init(){
//获取对象
TabBarView tabBar = findViewById(R.id.tab_bar);
//配置显示的内容
ArrayList items = new ArrayList<>();
//创建标签的对象
TabBarView.TabModel star = new TabBarView.TabModel(R.drawable.activity1,R.drawable.activity2,"活动");
TabBarView.TabModel friend = new TabBarView.TabModel(R.drawable.people2,R.drawable.people1,"好友");
items.add(star);
items.add(friend);
items.add(star);
items.add(friend);
tabBar.setItems(items);
}
}
图片资源::
动态 (1).png
动态.png
联系人 (1).png
联系人.png
消息 (1).png
消息.png