android自定义控件使用例子(头部Bar)

实现后的样式为:


第一步:
   定义XML布局:(navigation_bar.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title_bar"
    style="@style/title_bar_style">

    <LinearLayout
        android:id="@+id/top_left_container"
        android:layout_width="50dip"
        android:layout_height="fill_parent"
        android:paddingLeft="10.0dip"
        android:paddingRight="10.0dip">

        <ImageView
            android:id="@+id/top_left"
            android:layout_width="19dip"
            android:layout_height="19dip"
            android:layout_gravity="center_vertical"
            android:padding="2.0dip"
            android:src="@mipmap/top_navigation_back" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/top_right_container"
        android:layout_width="50dip"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dip"
        android:paddingLeft="10.0dip"
        android:paddingRight="10.0dip">

        <ImageView
            android:id="@+id/top_search"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:scaleType="centerInside"
            android:src="@mipmap/search_icon" />

    </LinearLayout>


    <TextView
        android:id="@+id/top_title"
        style="@style/title_text_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dip"
        android:layout_toRightOf="@+id/top_left_container"
        android:paddingBottom="1.5dip"
        android:paddingLeft="6.0dip"
        android:paddingRight="6.0dip"
        android:text="@string/app_name" />

</RelativeLayout>

第二步:
自定义要用到的控件的属性:(文件在res/values/attrs.xml)
<declare-styleable name="TopNavigationBar">
    <attr name="topbarTitle" format="string"></attr>
    <attr name="leftImBtnDrawable" format="reference"></attr>
    <attr name="rightImBtnDrawable" format="reference"></attr>
    <attr name="hideLeftImBtn" format="boolean" />
    <attr name="hideRightImBtn" format="boolean" />
</declare-styleable>
第三步:
处理自定义控件的事件
public class TopNavigationBar extends FrameLayout implements View.OnClickListener {
    private TextView mTopTitle;
    private LinearLayout mLeftContainerLl;
    private LinearLayout mRightContainerLl;
    private LeftBackClickListener leftBackClickListener;
    private RightSearchClickListener rightSearchClickListener;

    public TopNavigationBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TopNavigationBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TopNavigationBar);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.navigation_bar, this, true);
        CharSequence title = a.getText(R.styleable.TopNavigationBar_topbarTitle);

        mTopTitle = (TextView) findViewById(R.id.top_title);
        mLeftContainerLl = (LinearLayout) findViewById(R.id.top_left_container);
        mRightContainerLl = (LinearLayout) findViewById(R.id.top_right_container);
        mLeftContainerLl.setOnClickListener(this);
        mRightContainerLl.setOnClickListener(this);

        mTopTitle.setText(title);

        boolean isHidedLeftImBtn = a.getBoolean(R.styleable.TopNavigationBar_hideLeftImBtn, false);
        if (isHidedLeftImBtn) {
            mLeftContainerLl.setVisibility(GONE);
        }

        boolean isHidedRightImBtn = a.getBoolean(R.styleable.TopNavigationBar_hideRightImBtn, false);
        if (isHidedRightImBtn) {
            mRightContainerLl.setVisibility(GONE);
        }

        a.recycle();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_left_container: {
               leftBackClickListener.onLeftBtnClick();
                break;
            }
            case R.id.top_right_container: {
                rightSearchClickListener.onRightClick();
                break;
            }
            default:
                break;
        }

    }

    public void hindRight(){
        mRightContainerLl.setVisibility(GONE);
    }
    public void hindLeft(){
        mLeftContainerLl.setVisibility(GONE);
    }

    public void showRight(){
        mRightContainerLl.setVisibility(VISIBLE);
    }
    public void showLeft(){
        mLeftContainerLl.setVisibility(VISIBLE);
    }

    public void setOnLeftBackListener(LeftBackClickListener leftBackListener){
        this.leftBackClickListener = leftBackListener;
    }

    public void setOnRightSearchListener(RightSearchClickListener rightSearchListener){
        this.rightSearchClickListener = rightSearchListener;
    }

    public interface LeftBackClickListener{
        void onLeftBtnClick();
    }
    public interface RightSearchClickListener{
        void onRightClick();
    }

}

第四步:
使用该自定义的控件:
<com.luoshutao.mygraduateproject.utils.TopNavigationBar
    android:id="@+id/top_bar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    add:hideLeftImBtn="false"
    add:hideRightImBtn="true"
    add:leftImBtnDrawable="@mipmap/top_navigation_back"
    add:topbarTitle="找回成功">
</com.luoshutao.mygraduateproject.utils.TopNavigationBar>



  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值