一、概念
自定义组合控件就是多个控件组合起来成为一个新的控件
二、案例
实现自定义导航栏
1、源码
编写布局文件
<?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="50dp"
android:background="@color/colorAccent">
<ImageView
android:id="@+id/header_left_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="@drawable/ic_arrow_back_black_24dp"/>
<TextView
android:id="@+id/header_center_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ImageView
android:id="@+id/header_right_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/ic_cancel_small_dark"
android:layout_centerVertical="true"/>
</RelativeLayout>
设置自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TitleView">
<attr name="text_title" format="string"/>
</declare-styleable>
</resources>
继承RelativeLayout,实现构造方法
设置自定义属性以及对外提供的监听方法
public class TitleView extends RelativeLayout {
private ImageView img_left;
private TextView text_center;
private ImageView img_right;
public TitleView(Context context) {
super(context);
initView(context);
}
public TitleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView(context);
//自定义属性设置title
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleView);
String text_title = typedArray.getString(R.styleable.TitleView_text_title);
text_center.setText(text_title);
}
public TitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
layoutInflater.inflate(R.layout.title_layout, this,true);
img_left = findViewById(R.id.header_left_img);
text_center = findViewById(R.id.header_center_text);
img_right = findViewById(R.id.header_right_img);
}
//设置对外提供方法
public void setLeftListener(OnClickListener listener) {
img_left.setOnClickListener(listener);
}
//设置对外提供方法
public void setRightListener(OnClickListener listener) {
img_right.setOnClickListener(listener);
}
}
引用自定义控件
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.android.myview.TitleView
android:id="@+id/title"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:text_title="this is title"/>
</LinearLayout>
在主界面调用自定义View,并设置左右两边的点击事件
public class MainActivity extends AppCompatActivity{
private TitleView titleView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titleView = findViewById(R.id.title);
titleView.setLeftListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "left", 1000).show();
}
});
titleView.setRightListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "right", 1000).show();
}
});
}
}