标签:
最近需要做一个自定义的标题栏,参考了同事例子,写出自己的自定义控件,代码如下:
自定义控件TitleBar.java:
1 package com.example.hello; 2 3 import android.content.Context; 4 import android.util.AttributeSet; 5 import android.view.LayoutInflater; 6 import android.widget.Button; 7 import android.widget.RelativeLayout; 8 import android.widget.TextView; 9 10 public class TitleBar extends RelativeLayout{ 11 12 private Button btn; 13 private TextView tvBackTitle,tvTitle; 14 public TitleBar(Context context) { 15 super(context); 16 initView(context); 17 18 } 19 //一定要重写这个方法
20 public TitleBar(Context context, AttributeSet attrs) { 21 super(context, attrs); 22 initView(context); 23 } 24 25 private void initView(Context context) { 26 LayoutInflater.from(context).inflate(R.layout.title, this); 27 btn = (Button) findViewById(R.id.btnManageMoneyIncomeSearch); 28 tvBackTitle = (TextView) findViewById(R.id.tv_backTitle); 29 tvTitle = (TextView) findViewById(R.id.tvTitle); 30 } 31 32 public void setBackTitle(String title) { 33 tvBackTitle.setText(title); 34 } 35 36 public void setTitle(String title) { 37 tvTitle.setText(title); 38 } 39 40 public void setButtonText(String text) { 41 btn.setText(text); 42 } 43 44 public void setTvBackTitleListener(OnClickListener listener) { 45 tvBackTitle.setOnClickListener(listener); 46 } 47 48 public void setButtonListener(OnClickListener listener) { 49 btn.setOnClickListener(listener); 50 } 51 52 }
在界面中引用自定义控件:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.hello.TitleBar android:id="@+id/tb_main_title" android:layout_height="wrap_content" android:layout_width="match_parent"/> </RelativeLayout>
自定义控件的布局title.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:background="#647b97" android:paddingLeft="5dp" android:paddingRight="5dp"> <TextView android:id="@+id/tv_backTitle" android:layout_width="wrap_content" android:layout_height="match_parent" android:drawableLeft="@drawable/arrow_blue" android:drawablePadding="5dp" android:gravity="center_vertical" android:text="账户设置" android:textColor="@android:color/white" android:textSize="15sp" /> <TextView android:id="@+id/tvTitle" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_centerInParent="true" android:gravity="center_vertical" android:text="理财收益" android:textColor="@android:color/white" android:textSize="17sp" /> <Button android:id="@+id/btnManageMoneyIncomeSearch" android:layout_width="50dp" android:layout_height="40dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="#647b97" android:text="搜索" android:textSize="15sp" android:textColor="@android:color/white"/> </RelativeLayout>
标签:
原文地址:http://www.cnblogs.com/shizxq/p/4633411.html