安卓自定义控件

自定义布局文件:

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

    <Button
        android:id="@+id/button_title01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="back"
        android:textColor="@color/colorPrimary"
        android:background="#66ffff"
        android:layout_margin="5dp"
        android:layout_gravity="center"/>
    <EditText
        android:id="@+id/ET_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:maxLines="1"
        android:gravity="center"
        android:hint="Title text"
        android:textColor="@color/colorPrimary"
        android:textSize="24sp"/>

    <Button
        android:id="@+id/button_title02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Edit"
        android:textColor="@color/colorPrimary"
        android:background="#66ffff"
        android:layout_margin="5dp"
        android:layout_gravity="center"/>

</LinearLayout>

效果图
在这里插入图片描述

创建自定义控件:
这里重写了LinearLayout 带有两个参数的构造方法

public class TitleLayout extends LinearLayout {


    public TitleLayout (Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.activity_title,this);//第一个参数是要加载的布局文件id,第二个参数this表示父布局为TitleLayout 


	//设置点击事件
        Button button1=(Button) findViewById(R.id.button_title01);
        Button button2=(Button) findViewById(R.id.button_title02);
        final EditText editText=(EditText)findViewById(R.id.ET_title);
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity)getContext()).finish();
            }
        });

        button2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String data=editText.getText().toString();
                Toast.makeText(getContext(), "you click Edit button:"+data, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

在布局文件中添加这个自定义控件(自定义标题栏):

在first_layout.xml布局文件中添加以下代码,引入自定义控件:在添加自定义控件的时候,我们需要指明控件完整的类名,包名也不可省
如果导入的是布局文件使用:include标签官方文档

<com.example.activitytest.TitleLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

我们还需要取消自带的标题栏:
在FirstActivity中添加以下代码,屏蔽自带标题栏

  //隐藏标题栏
        ActionBar actionBar=getSupportActionBar();
        if (actionBar!=null)
        {
            actionBar.hide();
        }

最终效果图:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值