转载请注明出处:http://blog.csdn.net/htwhtw123/article/details/68485780
常常开发Android的APP时,大家经常遇到需要重复使用相同View的,而这些View只有一些细微的不同,比如上面的字不同,下面是仿照Android源码中定义View的方式,编写的一个自定义View,并在主活动的Layout中加载它,与之类似的可以在其他活动的布局中加载它。
以下为自定义标题栏举例(标题栏文字可在Activity里设置)(与之类似的,可以在这个标题栏添加按钮等控件,然后和这个TextView一样,在Activity中获取button对象,并对点击事件进行监听)
1.编写自己的布局,最外层使用任意的布局,不一定是ConstraintLayout,可以是LinearLayout等:
新建布局文件title:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorAccent">
<Button
android:id="@+id/left_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左按钮"
android:layout_marginLeft="2dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.0"/>
<TextView
android:id="@+id/title_tv"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintVertical_bias="0.0"
android:layout_marginBottom="8dp"/>
<Button
android:id="@+id/right_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右按钮"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginRight="16dp"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
2.建立自己的视图类,要求继承前面自定义布局最外层的类,定义内部空间变量,并获得对象,以供调用视图时直接访问,而如果是公共的属性,可以在这里直接定义,比如这里的左部按钮的退出事件 :
public class TitleView extends ConstraintLayout {
Button rightBt;
Button leftBt;
TextView titleText;
public TitleView(final Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
View titleView = LayoutInflater.from(context).inflate(R.layout.title_view, this);
leftBt = titleView.findViewById(R.id.left_bt);
leftBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "退出", Toast.LENGTH_SHORT).show();
((Activity)context).finish();
}
});
rightBt=titleView.findViewById(R.id.right_bt);
titleText=titleView.findViewById(R.id.title_tv);
}
}
3.加载布局:
在想要引入控件的Activity的布局文件中加入如下代码,其中“com.example.mylayout”为包名(不可省略):
<com.example.mylayout.TitleView
android:layout_width="match_parent"
android:layout_height="wrap_content"
></com.example.asus.growvegetables.TitleLayout>
4.想要添加控件的Activity中设置显示文字,获取自定义视图的对象,及其内部控件,对其进行定义:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TitleView titleView= (TitleView) findViewById(R.id.title_view);
titleView.rightBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "右边", Toast.LENGTH_SHORT).show();
}
});
}
}