Android自定义标题栏

在绝大部分的APP开发中都会用到标题栏,而且同一个项目中的标题栏的长的也相差不大,考虑到复用性决定把标题栏自定义一个View,当然也可以在每个xml中重复的粘贴同样的标题栏布局的代码。
这里定义一个TitleLayout继承RelativeLayout,一般的标题栏在左边和右边都会有一个imageView中间是一个TextView,如果是其他情况可以把不需要的控件直接隐藏掉就行。

public class TitleLayout extends RelativeLayout {
    private Context context;
    private RelativeLayout relativeLayout;
    private ImageView leftImageView, rightImageView;
    private TextView titleTextView;
    private TitleOnClickListener titleOnClickListener;

    public TitleLayout(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    public TitleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }
    //初始化view
    private void initView() {
        View view = LayoutInflater.from(context).inflate(R.layout.title_layout, null);
        leftImageView = (ImageView) view.findViewById(R.id.left_imageview);
        rightImageView = (ImageView) view.findViewById(R.id.right_imageview);
        titleTextView = (TextView) view.findViewById(R.id.title_textview);
        relativeLayout = (RelativeLayout) view.findViewById(R.id.titlelayout);
        this.addView(view);
    }
    //设置标题栏背景颜色
    public void setTitleBackgroundColor(int color) {
        relativeLayout.setBackgroundColor(color);
    }
    //设置标题
    public void setTitleMessage(String title) {
        titleTextView.setText(title);
    }
    //标题文字大小
    public void setTitleSize(float size) {
        titleTextView.setTextSize(size);
    }
    //设置左边图片
    public void setLeftImage(Drawable drawable) {
        leftImageView.setImageDrawable(drawable);
    }
    //设置右边图片
    public void setRightImage(Drawable drawable) {
        rightImageView.setImageDrawable(drawable);
    }
    //左边图片是否隐藏
    public void setLeftVisibility(int visibility) {
        leftImageView.setVisibility(visibility);
    }
    //右边图片是否隐藏
    public void setRightVisibility(int visibility) {
        rightImageView.setVisibility(visibility);
    }
    //标题文字是否隐藏
    public void setTitleVisibility(int visibility) {
        titleTextView.setVisibility(visibility);
    }
    //设置标题文字颜色
    public void setTitleColor(int color) {
        titleTextView.setTextColor(color);
    }
    //左边和右边imageview点击事件
    public void setListener(final TitleOnClickListener titleOnClickListener) {
        leftImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                titleOnClickListener.titleLeftOnClick();
            }
        });
        rightImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                titleOnClickListener.titleRightOnClick();
            }
        });
    }

}

定义一个接口来相应左边和右边imageView的点击事件,代码如下:

public interface TitleOnClickListener {
    void titleLeftOnClick();

    void titleRightOnClick();
}

接下来测试TitleLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.lzh.titlelayout.TitleLayout
        android:id="@+id/titlelayout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/DIMEN_TITLEHEIGHT">

    </com.example.lzh.titlelayout.TitleLayout>

</RelativeLayout>
public class MainActivity extends Activity {
    private TitleLayout titleLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        titleLayout = (TitleLayout) findViewById(R.id.titlelayout);
        titleLayout.setLeftImage(getResources().getDrawable(R.drawable.back));
        titleLayout.setRightImage(getResources().getDrawable(R.drawable.add));
        titleLayout.setTitleMessage("TitleLayout");
        titleLayout.setTitleSize(26);
        titleLayout.setTitleColor(getResources().getColor(R.color.white));
        titleLayout.setTitleBackgroundColor(getResources().getColor(R.color.bg_color));
        titleLayout.setListener(new TitleOnClickListener() {
            @Override
            public void titleLeftOnClick() {
                Toast.makeText(MainActivity.this, "点击了左边的图标", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void titleRightOnClick() {
                Toast.makeText(MainActivity.this, "点击了右边的图标", Toast.LENGTH_SHORT).show();
            }
        });
    }

效果图如下:
Android自定义标题栏_第1张图片
好了,这样一个简单的自定义标题栏就完成了!

你可能感兴趣的:(Android自定义标题栏)