美文网首页
在底部添加几个View控件

在底部添加几个View控件

作者: 26小瑜儿 | 来源:发表于2019-11-30 22:55 被阅读0次

运行效果:


image.png

在底部添加几个view控件
TabBarView

public class TabBarView extends ViewGroup {


    //保存所有标签的模型数据
    private ArrayList<TabModel> items;
    public TabBarView(Context context) {
        super(context);
    }

    public TabBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        //每个tab的尺寸
        float density = getResources().getDisplayMetrics().density;
        int width = (int) (50*density);
        //计算间距
        int padding = (getWidth()-width*items.size())/(items.size()+1);
        for (int j= 0; j < items.size(); j++) {
            //创建一个标签视图
            View v = new View(getContext());
            v.setBackgroundColor(Color.WHITE);
            //具体布局
            //距离左边的间距
            int left = (j+1)*padding+j*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<TabModel> items) {
        this.items = items;

    }

}

主界面

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<TabBarView.TabModel> 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);
    }
}

xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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">
    <swu.zht.a11_30test.TabBarView
        android:id="@+id/tab_bar"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="@color/colorAccent"

        >

    </swu.zht.a11_30test.TabBarView>


</RelativeLayout>

相关文章

网友评论

      本文标题:在底部添加几个View控件

      本文链接:https://www.haomeiwen.com/subject/ocfowctx.html