复用自定义布局TitleBar

Posted 源于未知

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了复用自定义布局TitleBar相关的知识,希望对你有一定的参考价值。

【第一种方案:addView】

TitleBar布局文件

<?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="#F34B4E"
    android:gravity="center_vertical">

    <LinearLayout
        android:id="@+id/llLeft"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center_vertical"
        android:onClick="finish"
        android:orientation="horizontal"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:clickable="false"
            android:drawableLeft="@drawable/icon_titlebar_left"
            android:drawablePadding="5dp"
            android:focusable="false"
            android:gravity="center"
            android:text="返回"
            android:textColor="#FFFFFF"
            android:textSize="16sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_centerInParent="true"
        android:ellipsize="end"
        android:gravity="center"
        android:singleLine="true"
        android:text=""
        android:textColor="#FFFFFF"
        android:textSize="18sp" />

    <RelativeLayout
        android:id="@+id/llRight"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:clickable="true"
        android:focusable="true"
        android:onClick="right"
        android:orientation="horizontal"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <ImageView
            android:id="@+id/ivRight"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:clickable="false"
            android:focusable="false"
            android:src="@drawable/icon_titlebar_more"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tvRight"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:clickable="false"
            android:focusable="false"
            android:gravity="center"
            android:onClick="right"
            android:textColor="#FFFFFF"
            android:textSize="16sp"
            android:visibility="visible" />
    </RelativeLayout>

</RelativeLayout>

BaseActivity:
public class BaseActivity extends AppCompatActivity 

    private View titleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
    

    public void finish(View view) 
        finish();
    

    public void right(View view) 
    

    public void addTitleBar(String title) 
        addTitleBar(title, null, 0);
    

    public void addTitleBar(String title, String rightText) 
        addTitleBar(title, rightText, 0);
    

    public void addTitleBar(String title, int rightIcon) 
        addTitleBar(title, null, rightIcon);
    

    private void addTitleBar(String title, String rightText, int rightIcon) 
        if (titleBar == null) 
            titleBar = LayoutInflater.from(this).inflate(R.layout.view_title_bar, null);
        
        TextView tvTitle = (TextView) titleBar.findViewById(R.id.tvTitle);
        TextView tvRight = (TextView) titleBar.findViewById(R.id.tvRight);
        ImageView ivRight = (ImageView) titleBar.findViewById(R.id.ivRight);
        tvTitle.setText(TextUtils.isEmpty(title) ? "" : title);
        tvRight.setText(TextUtils.isEmpty(rightText) ? "" : rightText);
        if (TextUtils.isEmpty(rightText) && rightIcon != 0) 
            ivRight.setVisibility(View.VISIBLE);
            ivRight.setImageResource(rightIcon);
        
        ViewGroup rootView = (ViewGroup) this.findViewById(android.R.id.content);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        rootView.addView(titleBar, layoutParams);
    
Activity中使用:
public class MainActivity extends BaseActivity 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    

    public void right(View view) 
        Toast.makeText(this, "点击了提交", Toast.LENGTH_SHORT).show();
    

    private void initView() 
        addTitleBar("注册", "提交");
//        addTitleBar("注册",R.drawable.icon_titlebar_more);
    

图片资源(图片是白色的,F12看或者鼠标右键另存为):
 
    
 
 
【第二种方案:自定义控件】

自定义控件TitleBar

public class TitleBar extends LinearLayout 

    private OnLeftClickListener mLeftClickListener;
    private OnRightClickListener mRightClickListener;
    private ViewHolder viewHolder;
    private View viewAppTitle;

    public TitleBar(Context context) 
        super(context);
        init();
    

    public TitleBar(Context context, AttributeSet attrs) 
        super(context, attrs);
        init();
    

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public TitleBar(Context context, AttributeSet attrs, int defStyle) 
        super(context, attrs, defStyle);
        init();
    

    private void init() 
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        viewAppTitle = inflater.inflate(R.layout.title_bar, null);
        this.addView(viewAppTitle, layoutParams);
        viewHolder = new ViewHolder(this);
        viewHolder.llLeft.setOnClickListener(new OnClickListener() 
            @Override
            public void onClick(View v) 
//                if (Utility.isFastDoubleClick()) 
//                    return;
//                

                if (mLeftClickListener != null) 
                    mLeftClickListener.onLeftClick(v);
                
            
        );
        viewHolder.llRight.setOnClickListener(new OnClickListener() 
            @Override
            public void onClick(View v) 
//                if (Utility.isFastDoubleClick()) 
//                    return;
//                

                if (mRightClickListener != null) 
                    mRightClickListener.OnRightClick(v);
                
            
        );
        initViewsVisible(true, true, true, true);
    

    public void initViewsVisible(boolean isLeftButtonVisile, boolean isCenterTitleVisile, boolean isRightIconVisile, boolean isRightTitleVisile) 
        // 左侧返回
        viewHolder.llLeft.setVisibility(isLeftButtonVisile ? View.VISIBLE : View.INVISIBLE);
        // 中间标题
        viewHolder.tvTitle.setVisibility(isCenterTitleVisile ? View.VISIBLE : View.INVISIBLE);
        // 右侧返回图标,文字
        if (!isRightIconVisile && !isRightTitleVisile) 
            viewHolder.llRight.setVisibility(View.INVISIBLE);
         else 
            viewHolder.llRight.setVisibility(View.VISIBLE);
        
        viewHolder.ivRight.setVisibility(isRightIconVisile ? View.VISIBLE : View.GONE);
        viewHolder.tvRight.setVisibility(isRightTitleVisile ? View.VISIBLE : View.INVISIBLE);
    

    public void setTitle(String title) 
        if (!TextUtils.isEmpty(title)) 
            viewHolder.tvTitle.setText(title);
        
    

    public void setRightTitle(String text) 
        if (!TextUtils.isEmpty(text)) 
            viewHolder.tvRight.setText(text);
        
    

    public void setRightIcon(int sourceID) 
        viewHolder.ivRight.setImageResource(sourceID);
    

    public void setLeftOnclick(OnLeftClickListener mOnLeftButtonClickListener) 
        if (mOnLeftButtonClickListener != null) 
        
    

    public void setBackground(int color) 
        viewAppTitle.setBackgroundColor(color);
    

    public void setOnLeftClickListener(OnLeftClickListener listen) 
        mLeftClickListener = listen;
    

    public void setOnRightClickListener(OnRightClickListener listen) 
        mRightClickListener = listen;
    

    public static abstract interface OnLeftClickListener 
        public abstract void onLeftClick(View v);
    

    public static abstract interface OnRightClickListener 
        public abstract void OnRightClick(View v);
    

    static class ViewHolder 
        LinearLayout llLeft;
        TextView tvTitle;
        RelativeLayout llRight;
        ImageView ivRight;
        TextView tvRight;

        public ViewHolder(View v) 
            llLeft = (LinearLayout) v.findViewById(R.id.llLeft);
            tvTitle = (TextView) v.findViewById(R.id.tvTitle);
            llRight = (RelativeLayout) v.findViewById(R.id.llRight);
            ivRight = (ImageView) v.findViewById(R.id.ivRight);
            tvRight = (TextView) v.findViewById(R.id.tvRight);
        
    
在布局中引用自定义控件

<?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="match_parent"
    android:orientation="vertical">

    <com.xiong.recyclerviewdemo.view.TitleBar
        android:id="@+id/titleBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
在Activity中使用:

    private void initView() 
        TitleBar titleBar = (TitleBar) findViewById(R.id.titleBar);
        titleBar.setTitle("注册");
        titleBar.setRightTitle("确认");
        titleBar.setBackground(Color.parseColor("#F34B4E"));
//        titleBar.setRightIcon(R.drawable.icon_titlebar_more);
        //返回
        titleBar.setOnLeftClickListener(new TitleBar.OnLeftClickListener() 
            @Override
            public void onLeftClick(View v) 
                finish();
            
        );
        //提交
        titleBar.setOnRightClickListener(new TitleBar.OnRightClickListener() 
            @Override
            public void OnRightClick(View v) 

            
        );
    

效果图:





以上是关于复用自定义布局TitleBar的主要内容,如果未能解决你的问题,请参考以下文章

如何修改android标题栏界面

可复用的自定义Adapter

无法单击 UWP 自定义标题栏按钮

调整手机titlebar与app的titlebar相衔接

自定义控件和使用的两种基本方法

与活动风格不同的日期选择器