[Android新手学习笔记22]-如何自定义控件

来源:互联网 发布:大蒜种植面积数据 编辑:程序博客网 时间:2024/06/04 22:47

1.引入布局文件

右键res/layout文件夹,创建Layout Resource File,命名为title。配置代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="horizontal" android:layout_width="match_parent"
  4.    android:layout_height="60dp"
  5.    android:background="#00ff00">
  6.    <Button
  7.        android:id="@+id/back_button"
  8.        android:text="返回"
  9.        android:textSize="30sp"
  10.        android:layout_gravity="center"
  11.        android:layout_width="wrap_content"
  12.        android:layout_height="60dp" />
  13.    <TextView
  14.        android:id="@+id/text_view"
  15.        android:text="标题"
  16.        android:textSize="30sp"
  17.        android:layout_gravity="center"
  18.        android:gravity="center"
  19.        android:layout_width="0dp"
  20.        android:layout_weight="1"
  21.        android:layout_height="60dp" />
  22.    <Button
  23.        android:id="@+id/edit_button"
  24.        android:text="编辑"
  25.        android:textSize="30sp"
  26.        android:layout_gravity="center"
  27.        android:layout_width="wrap_content"
  28.        android:layout_height="60dp" />
  29. </LinearLayout>

android:layout_gravity控制控件内容排列方式。

android:gravity控制控件排列方式。

然后在通过<include>标签引入,代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:layout_width="match_parent"
  5.    android:layout_height="match_parent">
  6.    <include layout="@layout/title" />
  7. </LinearLayout>

在引用控件的Activity中,去掉标题栏,代码如下:

  1. public class MainActivity extends AppCompatActivity {
  2.    @Override
  3.    protected void onCreate(Bundle savedInstanceState) {
  4.        super.onCreate(savedInstanceState);
  5.        setContentView(R.layout.activity_main);
  6.        ActionBar actionBar = getSupportActionBar();
  7.        if (actionBar != null) {
  8.            actionBar.hide();
  9.        }
  10.    }
  11. }

2.添加方法

修改引入布局文件,代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:layout_width="match_parent"
  5.    android:layout_height="match_parent">
  6.    <top.xiexiaodong.uidemo.TitleLayout
  7.        android:layout_width="match_parent"
  8.        android:layout_height="wrap_content" />
  9. </LinearLayout>

创建TitleLayout类,继承LinearLayout类,在这个类里面实现控件事件,代码如下:

  1. public class TitleLayout extends LinearLayout {
  2.    public TitleLayout(Context context, AttributeSet attrs) {
  3.        super(context, attrs);
  4.        LayoutInflater.from(context).inflate(R.layout.title, this);
  5.        Button backButton = (Button) findViewById(R.id.back_button);
  6.        Button editButton = (Button) findViewById(R.id.edit_button);
  7.        backButton.setOnClickListener(new OnClickListener() {
  8.            @Override
  9.            public void onClick(View v) {
  10.                ((Activity) getContext()).finish();
  11.            }
  12.        });
  13.        editButton.setOnClickListener(new OnClickListener() {
  14.            @Override
  15.            public void onClick(View v) {
  16.                Toast.makeText(getContext(), "你点击了编辑按钮", Toast.LENGTH_SHORT).show();
  17.            }
  18.        });
  19.    }
  20. }

0 0