1. 自定义控件

如下图所示,android中国所有控件都是直接或间接继承自View的,View是android中最基本的一种UI组件,它可以在屏幕上绘制一块矩形区域,并能响应这种区域的各种事件。
ViewGroup是一种特殊的View,它可以包含各种子View和子ViewGroup,是一个用于放置控件和布局的容器。

44.png

1. 引入布局

创建标题布局
title.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:background="@drawable/title_bg">
  6. <Button
  7. android:id="@+id/title_back"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center"
  11. android:layout_margin="5dp"
  12. android:background="@drawable/back_bg"
  13. android:text="Back"
  14. android:textColor="#fff"></Button>
  15. <TextView
  16. android:id="@+id/title_text"
  17. android:layout_width="0dp"
  18. android:layout_height="wrap_content"
  19. android:layout_gravity="center"
  20. android:gravity="center"
  21. android:layout_weight="1"
  22. android:text="Title Text"
  23. android:textColor="#fff"
  24. android:textSize="24sp">
  25. </TextView>
  26. <Button
  27. android:id="@+id/title_edit"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_gravity="center"
  31. android:layout_margin="5dp"
  32. android:background="@drawable/edit_bg"
  33. android:text="Edit"
  34. android:textColor="#fff">
  35. </Button>
  36. </LinearLayout>

引入标题布局
activity_main.xml

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

隐藏标题栏
MainActivity

  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. }

结果
55.png

2. 创建自定义控件

com.example.uicustomviews.TitleLayout

  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 titleBack = (Button) findViewById(R.id.title_back);
  6. Button titleEdit = (Button) findViewById(R.id.title_edit);
  7. titleBack.setOnClickListener(new OnClickListener() {
  8. @Override
  9. public void onClick(View v) {
  10. ((Activity) getContext()).finish();
  11. }
  12. });
  13. titleEdit.setOnClickListener(new OnClickListener() {
  14. @Override
  15. public void onClick(View v) {
  16. Toast.makeText(getContext(), "you clicked edit button ", Toast.LENGTH_SHORT).show();
  17. }
  18. });
  19. }
  20. }

activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content">
  4. <!--<include layout="@layout/title"/>-->
  5. <com.example.uicustomviews.TitleLayout
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content">
  8. </com.example.uicustomviews.TitleLayout>
  9. </LinearLayout>

2. ListView

1. 适配器

2. ListView的优化

3. RecycleView

1. 适配器

2. 水平布局