BottomBar有文字的情况

来源:互联网 发布:suse linux 扩磁盘 编辑:程序博客网 时间:2024/05/07 16:05
在BottomBarTab中package com.example.hello.lianxiday_041101.view;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Color;import android.graphics.drawable.Drawable;import android.support.annotation.DrawableRes;import android.support.v4.content.ContextCompat;import android.util.AttributeSet;import android.util.TypedValue;import android.view.Gravity;import android.view.ViewGroup;import android.widget.FrameLayout;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import com.example.hello.lianxiday_041101.R;/** * Created by Administrator on 6/15 0015. */public class BottomBarTab extends FrameLayout {    private ImageView mIcon;    private int mTabPosition = -1;    private Context mContext;    private TextView textView;    public BottomBarTab(Context context, @DrawableRes int icon, CharSequence title) {        this(context, null, icon,title);    }    public BottomBarTab(Context context, AttributeSet attrs, @DrawableRes int icon, CharSequence title) {        this(context, attrs, 0, icon,title);    }    public BottomBarTab(Context context, AttributeSet attrs, int defStyleAttr, @DrawableRes int icon, CharSequence title) {        super(context, attrs, defStyleAttr);        init(context, icon,title);    }    private void init(Context context, int icon, CharSequence title) {        this.mContext = context;        TypedArray typedArray = context.obtainStyledAttributes(                new int[]{R.attr.selectableItemBackgroundBorderless});        Drawable drawable = typedArray.getDrawable(0);        setBackgroundDrawable(drawable);        typedArray.recycle();        LinearLayout lLContainer = new LinearLayout(context);        lLContainer.setOrientation(LinearLayout.VERTICAL);        lLContainer.setGravity(Gravity.CENTER);        LayoutParams paramsContainer = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);        paramsContainer.gravity = Gravity.CENTER;        lLContainer.setLayoutParams(paramsContainer);        mIcon = new ImageView(context);        int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 27, getResources().getDisplayMetrics());        LayoutParams params = new LayoutParams(size, size);        params.gravity = Gravity.CENTER;        mIcon.setImageResource(icon);        mIcon.setLayoutParams(params);        mIcon.setColorFilter(Color.parseColor("#c9c9c9"));        lLContainer.addView(mIcon);        textView = new TextView(context);        textView.setText(title);        LinearLayout.LayoutParams params1=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);        params1.topMargin =(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());        textView.setTextSize(10);        textView.setTextColor(ContextCompat.getColor(context, R.color.tab_unselect));        textView.setLayoutParams(params1);        lLContainer.addView(textView);        addView(lLContainer);    }    @Override    public void setSelected(boolean selected) {        super.setSelected(selected);        if (selected) {            mIcon.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary));            textView.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));        } else {            mIcon.setColorFilter(ContextCompat.getColor(mContext, R.color.tab_unselect));            textView.setTextColor(ContextCompat.getColor(mContext, R.color.tab_unselect));        }    }    public void setTabPosition(int position) {        mTabPosition = position;        if (position == 0) {            setSelected(true);        }    }    public int getTabPosition() {        return mTabPosition;    }}
0 0
原创粉丝点击