我的android_code_lib:自定义标题栏

==android自带的标题略有点难看,写代码写累了。自己总结下吧。

其实也不算完全的原创,这是看过guolin大大的blog之后才想得到的。

首先,建立一个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="wrap_content"
    android:background="#A4D3EE" >

    <!-- 左侧按钮,具体参数可以自己设定 -->
    <Button
        android:id="@+id/bt_left"
        android:layout_width="60sp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_margin="5sp"
        android:text="left"
        android:textColor="#fff"
        android:textSize="15sp" />

    <!-- 中间的标题文本 -->
    <TextView
        android:id="@+id/titleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="5sp"
        android:text="title"
        android:textColor="#fff"
        android:textSize="30sp" />

    <!-- 右侧按钮 -->
    <Button
        android:id="@+id/bt_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_margin="5sp"
        android:text="right"
        android:textColor="#fff"
        android:textSize="15sp" />

</RelativeLayout>

再建立一个java文件,继承自FrameLayout。

public class TitleBar extends FrameLayout{
	
	private Button btLeft, btRight;
	private TextView titleText;
	public TitleBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		//动态载入标题栏的xml文件
		LayoutInflater.from(context).inflate(R.layout.title_bar, this);
		//获取标题栏的左侧按钮,右侧按钮,以及中间的标题文本
		titleText = (TextView)findViewById(R.id.titleText);
		btLeft = (Button) findViewById(R.id.bt_left);
		btRight = (Button) findViewById(R.id.bt_right);
	}
	// 设置标题文字
	public void setTitleText(String text) {
		this.titleText.setText(text);
	}
	// 设置左按钮文字
	public void setBtLeftText(String text) {
		this.btLeft.setText(text);
	}
	// 设置右按钮文字
	public void setBtRightText(String text) {
		this.btRight.setText(text);
	}
	// 设置左侧按钮的监听事件
	public void setBtLeftListener(OnClickListener l){
		this.btLeft.setOnClickListener(l);
	}
	// 设置右侧按钮的监听事件
	public void setBtRightListener(OnClickListener l){
		this.btRight.setOnClickListener(l);
	}
	
	// 隐藏左侧按钮
	public void hideLeftButton(){
		btLeft.setVisibility(8);
	}
	// 隐藏右侧按钮
	public void hideRightButton(){
		btRight.setVisibility(8);
	}
}

接着在activity的布局文件中,(一般是最顶部拉),放入标题栏

<?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="wrap_content"
    android:background="#A4D3EE" >

    这里要写的是TitleBar类的包名和类名,其余操作与普通的控件无异
    <packageName.TitleBar
        android:id="@+id/title_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</RelativeLayout>

在Activity中,和找到普通控件的方法一样,用findViewById方法找到该控件:

接下来就可以进行一系列的操作了,比如设置标题栏的文字等等。

TitleBar titleBar;
titleBar = (TitleBar)findViewById(R.id.title_bar);


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值