android自定义控件使用例子(头部Bar)
来源:互联网 发布:java sessionscoped 编辑:程序博客网 时间:2024/06/05 03:59
实现后的样式为:
定义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 0
- android自定义控件使用例子(头部Bar)
- 自定义tab bar控件
- 自定义控件--头部视差
- Android自定义控件,可以滑动的导航栏(Navigation Bar)
- Android自定义title bar(标题栏)
- Android自定义title bar(标题栏)
- Android 自定义Progress Bar
- android 自定义title bar
- Android 自定义Progress Bar
- android 自定义title bar
- 自定义android Rating bar
- android自定义title bar
- Android 自定义Progress Bar
- Android自定义Title Bar
- Android滑动头部控件
- 替换Android控件默认样式 (例子:自定义进度条)
- 自绘控件:自定义Tab Bar控件
- 自绘控件:自定义Tab Bar控件
- n个正整数,联接成一排,组成一个最小(最大)的多位整数
- 关于Android Studio快捷键的那些事儿
- 简易计算器之换主题
- dump函数以及数字类型浅析
- BZOJ3060/POI 2012 Tour de Byteotia
- android自定义控件使用例子(头部Bar)
- MIT牛人解说数学体系
- 【数论】乘法逆元
- easyUI中textArea控制输入字符的多少
- xml介绍
- $.ajax $.getJSON在java中的跨越解决方案
- 48. Rotate Image
- Activity简单几步支持向右滑动返回
- char类型和int型可以通用