自定义View---header(标题栏)
2017-07-21 21:51
204 查看
布局文件,xml代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/colorPrimary" android:orientation="horizontal"> <ImageView android:id="@+id/back" android:src="@drawable/ic_back" android:padding="10dp" android:layout_width="50dp" android:layout_height="50dp" /> <TextView android:id="@+id/title" android:layout_weight="1" android:text="题目" android:textSize="20sp" android:gravity="center" android:textColor="@color/white" android:layout_width="wrap_content" android:layout_height="match_parent" /> <ImageView android:id="@+id/fun" android:src="@mipmap/ic_search_white" android:padding="6dp" android:layout_width="50dp" android:layout_height="50dp" /> </LinearLayout>
资源文件,attrs代码
<declare-styleable name="Header"> <attr name="header_back" format="integer"/> <attr name="header_title" format="string"/> <attr name="header_fun" format="integer"/> </declare-styleable>
java代码
public class Header extends LinearLayout{ private Context context; private AttributeSet attrs; private String title; private Integer back; private Integer fun; private View v; ImageView iv_back; TextView tv_title; ImageView iv_fun; public Header(Context context, @Nullable AttributeSet attrs) { super(context, attrs); this.context = context; this.attrs = attrs; initView(); } private void initView(){ TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Header); title = typedArray.getString(R.styleable.Header_header_title); back = typedArray.getInteger(R.styleable.Header_header_back,0); fun = typedArray.getInteger(R.styleable.Header_header_fun,0); typedArray.recycle(); v = LayoutInflater.from(context).inflate(R.layout.view_header,null); iv_back = (ImageView) v.findViewById(R.id.back); tv_title = (TextView) v.findViewById(R.id.title); iv_fun = (ImageView) v.findViewById(R.id.fun); setTitle(title); LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); addView(v,params); iv_back.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ((Activity)context).finish(); } }); } //设置题目 public void setTitle(String title){ tv_title.setText(title); } //设置返回按钮 public void setOnBackClickListener(OnClickListener o){ iv_back.setOnClickListener(o); } //设置fun按钮 public void setOnFunClickListener(OnClickListener o){ iv_fun.setOnClickListener(o); } }
使用
<com.view.Header android:id="@+id/header" app:header_title="用户" android:layout_width="match_parent" android:layout_height="wrap_content"/>
相关文章推荐
- 自定义普通文本TableView header footer 视图
- ios-day09-02(QQ好友列表。自定义UITableViewHeaderFooterView、如何控制UITableView每一组的展开和闭合、代理的使用)
- 自定义viewForHeaderInSection、自定义组头的显示
- iOS 设置一行颜色不同的NSString 、剪切图片、设置TableView的自定义header
- RecyclerView有标题栏的分类列表 自定义列数(不同行设置不同个子项item)
- 自定义View之自定义标题栏
- listview添加headerview需要注意的,listview自定义的item注意的焦点
- OC笔记 - 自定义tableView的headerView(footerView)(2015.3.13)
- 为我们的UITableViewFooterHeaderView添加自定义View的布局
- Android 实现横向标题栏滚动效果(HorizontalScrollView + GridView + Viewpager + 自定义适配器)
- 拓展自定义下拉缩放header的组件PullZoomView,增加对listView的下拉刷新和上拉更多
- [Android]自定义View标题栏TitleBar
- 自定义带网络请求的UITableView中tableHeaderView视图
- 自定义view-下拉时标题栏渐入
- Android自定义View之PinnedHeaderListView
- Android自定义view----顶部标题栏2
- 组合View-标题栏自绘控件圆形进度条/扫描二维码/自定义ViewGroup阶梯布局
- HeaderView、FooterView、刷新、加载更多,支持自定义组装的RecyclerView组件
- 自定义tableview Header和Footer的样式
- 8/25/自定义Adapter缓存与时间优化/ListView、checkBox点击监听/addHeaderView/addFootView