Day7导航栏的实现

1.MainActivity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
        // test();
        }
    private void test(){
        //代码方式创建控件
        NavigationBar bar=new NavigationBar(this);
        //设置背景颜色
        bar.setBackground(Color.MAGENTA);
        //设置需要返回按钮
        bar.setShow_Back(true,"返回1");
        //让当前这个控件作为activity的主界面
        setContentView(bar);
    }
    /**
     *
     */
}

2.NavigationBar

public class NavigationBar extends LinearLayout {
    //保存外部设置的背景颜色 默认灰色
    private int background =Color.DKGRAY;
    //记录是否需要返回按钮
    private boolean show_Back=false;

    //使用java代码创建控件
    public NavigationBar(Context context) {
       this(context,null);
    }

    //使用xml创建控件
    public NavigationBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }
 public int getbackground(){
        return  background;
 }
 public void setBackground(int background){
        this.background=background;
        //将外部传递过来的颜色设置为背景颜色
        setBackgroundColor(background);
 }
    private void init(Context context,AttributeSet attrs) {
        //设置横向布局
        setOrientation(LinearLayout.HORIZONTAL);
        //设置背景
        setBackgroundColor(Color.GRAY);
        //设置内容垂直居中
        setGravity(Gravity.CENTER_VERTICAL);
        //判断是不是xml配置的
        if(attrs!=null){
            //attrs里面提取 xml里面配置的所有属性
           TypedArray typedArray= context.obtainStyledAttributes
                   (attrs,R.styleable.NavigationBar);
           //提取自己需要的属性
      int color= typedArray.getColor(R.styleable.NavigationBar_hsl_background,Color.MAGENTA);
      //取是否需要返回按钮
          boolean show=  typedArray.getBoolean(R.styleable.NavigationBar_show_back,false);
          //取返回按钮的标题
            String title=typedArray.getString(R.styleable.NavigationBar_back_title);
              //使用数据
            setBackground(color);
          setShow_Back(show,title);

        }
    }

    public boolean isShow_Back() {
        return show_Back;
    }

    public void setShow_Back(boolean show_Back,String title) {
        this.show_Back = show_Back;
        if(show_Back==true){
            //创建返回按钮
            Button back=new Button(getContext());
            //设置按钮的布局属性
            LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            params.leftMargin=(int)(10*getResources().getDisplayMetrics().density);
            //设置标题
            if(title!=null){
                back.setText(title);
            }
            else{
                back.setText("back");
            }
            //添加控件
            addView(back,params);
        }
    }
}

3.自定义xml

image.png

4.实现效果(可点击)

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。