1.自定义标题栏,并引用

首先新建一个title布局写下如下代码

<?xml version="1.0" encoding="utf-8"?&gt;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"&gt;
<Button
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:background="@drawable/back"
    android:layout_marginLeft="10dp"
    android:layout_gravity="center"
    android:id="@+id/back"/&gt;
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="title text"
        android:textSize="22sp"/>
    <Button
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/edit"
        android:id="@+id/edit"/>

</LinearLayout>

效果如下

 

 

然后在其他布局中引用该布局如下

<?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"
    >
    <include layout="@layout/title"/>



</LinearLayout>

效果如下

 

 

activity隐藏原来的标题显示新的标题代码如下:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar=getSupportActionBar();//获取actionBar实例
        if(actionBar!=null){
            actionBar.hide();//将标题栏隐藏
        }
    }
}

最后效果如下:

 2.自定义控件

新建TitleLayout继承LinearLayout,让他成为定义标题栏控件代码如下:

public class TitleLayout extends LinearLayout {
    public TitleLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.title,this);//动态加载布局文件
    }
}

然后在布局文件添加定义控件,代码如下:

<?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"
    >
    <com.example.chuangjianzidingyikongjian.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    
</LinearLayout>

之后修改TitleLayout对标题栏中的按钮添加注册点击事件

public class TitleLayout extends LinearLayout {
    public TitleLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.title,this);//动态加载布局文件
        Button back=findViewById(R.id.back);
        Button edit=findViewById(R.id.edit);
        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
               // Toast.makeText(getContext(),"back",Toast.LENGTH_SHORT).show();
                ((Activity)getContext()).finish();
            }
        });
        edit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(),"edit",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

原文地址:https://blog.csdn.net/m0_61917410/article/details/122623230

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_16513.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注