android怎么自定义view android


android如何自定view呢?不清楚的小伙伴们一起来看看我现在的分享!
android可以根据组合控件来建立自定view 。组合控件便是将系统软件原来的控件开展组合 , 组成一个新的控件 。这类形式下 , 不需要开发者自身去绘制图上展示的內容 , 也不需要开发者调用onMeasure , onLayout , onDraw方式来完成精确测量、布局及其draw步骤 。
具体步骤:
1、界定标题栏布局文档
界定标题栏的布局文档custom_title_view.xml , 将返回按钮和文章标题文字开展组合 。这一步用以明确标题栏的模样 , 编码如下所示所显示:
<?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="wrap_content"
android:background="@android:color/holo_orange_light">
<Button
android:id="@ id/btn_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:text="Back"
android:textColor="@android:color/white" />
<TextView
android:id="@ id/title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Title"
android:textColor="@android:color/white"
android:textSize="20sp" />
</RelativeLayout>
2、依据给出布局完成自定View
public class CustomTitleView extends FrameLayout implements View.OnClickListener {
private View.OnClickListener mLeftOnClickListener;
private Button mBackBtn;
private TextView mTiTTLeView;
public CustomTitleView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.custom_title_view, this);
mBackBtn = findViewById(R.id.btn_left);
mBackBtn.setOnClickListener(this);
mTittleView = findViewById(R.id.title_tv);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_left:
if (mLeftOnClickListener != null) {
mLeftOnClickListener.onClick(v);
}
break;
}
}
public void setLeftOnClickListener(View.OnClickListener leftOnClickListener) {
mLeftOnClickListener = leftOnClickListener;
}
public void setTittle(String title){
mTittleView.setText(title);
}
}
表明:
(1)编码中对外开放给予了2个插口 , 一是动态性设定文章标题 , 二是使用人可以自定返回按钮的点击事件 。
(2)CustomTitleView的构造方法 , 要挑选2个主要参数的 , 挑选其他主要参数的构造方法会出错 。这一点是小编开发设计机检测的結果 , 临时不清楚是否全部手机全是那样 。
(3)这儿是承继的FrameLayout , 可是承继LinearLayout , RelativeLayout等系统软件布局控件都能够 。往往要承继这种系统软件现有的ViewGroup , 是由于那样可以不需要再调用onMeasure , onLayout等 , 那样方便许多 。因为这儿是一个布局控件 , 得用LayoutInflater来添充 , 因此必须承继ViewGroup , 假如承继View的立即派生类 , 编译程序会不通过 。因此 , CustomTitleView自身是一个器皿 , 彻底可以当做器皿应用 , 这时CustomTitleView本身的信息会和其做为父布局加上的子控件 , 实际效果会累加 , 实际的堆叠实际效果是依据承继的器皿特点影响的 。
3、在Activity的布局文档中加上CustomTitleView 。
在Activity的布局文档activity_custom_view_compose_demo.xml中 , 像应用系统软件控件一样应用CustomTitleView就可以 。CustomTitleView自身便是承继的现有的系统软件布局 , 因此他们有着的特性特点 , CustomTitleView一样有着 。
<?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="match_parent">
<com.example.demos.customviewdemo.CustomTitleView
android:id="@ id/customview_title"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.demos.customviewdemo.CustomTitleView>
</RelativeLayout>
4、在Activity中实际操作CustomTitleView , 编码如下所示:
1 public class CustomViewComposeDemoActivity extends AppCompatActivity { 2 3private CustomTitleView mCustomTitleView; 4@Override 5protected void onCreate(Bundle savedInStanceState) { 6super.onCreate(savedInstanceState); 7setContentView(R.layout.activity_custom_view_compose_demo); 8mCustomTitleView = findViewById(R.id.customview_title); 9mCustomTitleView.setTittle("This is Title");10mCustomTitleView.setLeftOnClickListener(new View.OnClickListener() {11@Override12public void onClick(View v) {13finish();14}15});16 17}18 }
在第8行中 , 获得到CustomTitleView案例 , 第9行设定标题文字 , 第10行自定“Back”按键点击事件 。
5、设计效果图
依照以上的4步 , 就根据组合控件完成了一个较为简单的自定标题栏 。
以上便是我现在的共享了 , 期待可以作用到大伙儿 。
【android怎么自定义view android】