自定义的标题栏和隐藏ActionBar

首先要了解什么事状态栏什么事actionbar这里我们要隐藏的是ActionBar不是状态栏

在AndroidManifest.xml里面改变 application的theme的属性改变成@style/Theme.AppCompat.Light.NoActionBar
现在的android studio里面的类多数是继承的AppCompatActivity所以在使用之前隐藏activity的方发就不好使了
下面讲一下自定义的一个标题栏
首先创建一个类继承 RelativeLayout
就拿今日头条的标题栏做例子

先看一下布局很简单
<?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="42dp"
android:background="#c83c23"
>

<ImageView
android:layout_marginLeft="12dp"
android:id="@+id/title_img_left"
android:layout_width="42dp"
android:layout_height="42dp"
android:src="@drawable/default_sdk_login"
android:layout_alignParentLeft="true"
/>

<TextView
android:layout_toLeftOf="@+id/title_img_right"
android:layout_toRightOf="@+id/title_img_left"
android:id="@+id/title_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="今日头条"
android:textColor="#fff"
android:textSize="24dp"
/>
<ImageView
android:id="@+id/title_img_right"
android:layout_width="42dp"
android:layout_height="match_parent"
android:src="@drawable/search_toast_ab"
android:layout_alignParentRight="true"
android:layout_marginRight="12dp"
/>

</RelativeLayout>
这是布局的文件
下面看一下代码
public class Title_View extends RelativeLayout {
private TextView title_text;
private ImageView title_img_left;

public Title_View(Context context) {
super(context);
initview(context);
}

private void initview(Context context) {
LayoutInflater. from (context).inflate(R.layout. activity_title , this);
title_text = (TextView) findViewById(R.id. title_text );
title_img_left= (ImageView) findViewById(R.id. title_img_left );
}

//设置左侧imageview点击事件
public void setLeftlistener(OnClickListener listener) {
findViewById(R.id. title_img_left ).setOnClickListener(listener);
}


//设置右侧imageview点击事件
public void setRightlistener(OnClickListener listener) {
findViewById(R.id. title_img_right ).setOnClickListener(listener);
}

//设置标题
public void setTitle(String text) {
title_text.setText(text);
}


public Title_View(Context context, AttributeSet attrs) {
super(context, attrs);
initview(context);
}

public Title_View(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}

在要使用这个标题的布局文件的地方调用

如果要设施点击事件的话就直接调用你自定义类里的方法就可以

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值