简单的自定义标题栏(不使用Toolbar)

这篇博客介绍了如何不使用Toolbar,通过创建一个BaseView并封装成标题栏组件,详细讲解了布局设计和在Activity中的应用,支持图标更换和背景色自定义,提供了可扩展的预留控件以适应更多需求。
摘要由CSDN通过智能技术生成

比较简单的自定义标题栏,这里直接封装成一个类似控件的样子,先上效果图:


我们可以先写一个BaseView来,用来做标题栏的基础布局:

/**
 * 自定义View的基类
 * @author
 */
public class BaseView extends FrameLayout implements OnClickListener {
	protected Activity mActivity;
	protected Context mContext;
	protected View mView;
	protected LayoutInflater mInflater;
	private TextView errorTV;
	private boolean isErrorViewShow;

	public BaseView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

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

	public BaseView(Context context) {
		super(context);
	}

	protected void setContentView(int layoutId) {

		mContext = getContext();
		if (mContext instanceof Activity) {
			mActivity = (Activity) mContext;
		}

		mInflater = LayoutInflater.from(mContext);
		mView = mInflater.inflate(layoutId, null);
		addView(mView);
	}

	protected boolean setContentView(int layoutId,int color) {
                //这个方法用于后面完善操作
		mContext = getContext();
		if (mContext instanceof Activity) {
			mActivity = (Activity) mContext;
		}

		mInflater = LayoutInflater.from(mContext);
		mView = mInflater.inflate(layoutId, null);
		mView.setBackgroundColor(color);
		addView(mView);  return true;  
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	public void addErrorView(Context context, String text){
		if (null == errorTV) {
			FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			layoutParams.gravity = Gravity.CENTER;
			
			errorTV = new TextView(context);
			errorTV.setLayoutParams(layoutParams);
			errorTV.setVisibility(View.VISIBLE);
						
			errorTV.setTextSize(15);
			errorTV.setTextColor(Color.GRAY);
		}
		errorTV.setText(text);
		addView(errorTV);
		isErrorViewShow = true;
	}
	
	public void removeErrorView(){
		if (null != errorTV) {
			removeView(errorTV);
		}
		isErrorViewShow = false;
	}
	
	public boolean isErrorViewShow(){
		return isErrorViewShow;
	}

}


然后让真正的标题栏布局继承上面的类:

/**
 * Created by lan.zheng on 2016/9/19.
 */
public class CommonTitleView extends BaseView{
    private ImageButton backIBtn;
    private TextView leftTV;
    private TextView titleTV;
    private ImageButton addIBtn;
    private TextView rightTV;

    p
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想要自定义标题栏,可以在布局文件中添加一个 `Toolbar` 元素,然后在 Activity 的 `onCreate` 方法中使用 `setSupportActionBar` 方法将该 `Toolbar` 对象设置为 Activity 的标题栏。具体的步骤如下: 1. 在布局文件中添加 `Toolbar` 元素,例如: ``` <androidx.appcompat.widget.Toolbar android:id="@+id/my_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:titleTextColor="@android:color/white" android:title="My Toolbar" /> ``` 在这个例子中,我们使用 `androidx.appcompat.widget.Toolbar` 元素创建一个 ToolbarToolbar 的 id 为 `my_toolbar`,高度为 ActionBar 的高度,背景为应用程序的主题色,标题为 "My Toolbar"。 2. 在 Activity 的 `onCreate` 方法中使用 `setSupportActionBar` 方法将该 `Toolbar` 对象设置为 Activity 的标题栏,例如: ``` public class MyActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_activity_layout); Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); setSupportActionBar(myToolbar); } } ``` 在这个例子中,我们首先通过 `findViewById` 方法获取到布局文件中的 `Toolbar` 对象 `my_toolbar`,然后使用 `setSupportActionBar` 方法将该 `Toolbar` 对象设置为 Activity 的标题栏。 这样就可以在 Activity 中自定义标题栏了。注意,如果您使用自定义标题栏,就不需要在 AndroidManifest.xml 文件中设置 `android:label` 属性来显示默认的标题栏了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值