自定义View

来源:互联网 发布:淘宝网高跟鞋 编辑:程序博客网 时间:2024/06/08 16:07
public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }}第一种方法:继承View实现自定义View
public class MyView extends View {    private String text;    private int textColor;    private float textSize;    public MyView(Context context) {        super(context);        init(null);    }    public MyView(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);        init(attrs);    }    //创建方法    private void init(@Nullable AttributeSet attrs){        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyView);        text = typedArray.getString(R.styleable.MyView_text);        textColor=typedArray.getColor(R.styleable.MyView_textcolor,0xffff);        textSize=typedArray.getDimension(R.styleable.MyView_textsize,16);    }    //画   布局设置    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        Paint paint=new Paint();        paint.setAntiAlias(true);        paint.setColor(textColor);        paint.setTextSize(textSize);        canvas.drawText(text,10,100,paint);    }}values下attrs:(第一种方法用)
<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="MyView">    <attr name="text" format="string"></attr>    <attr name="textsize" format="dimension"></attr>    <attr name="textcolor" format="color"></attr></declare-styleable></resources>

第二种方法:继承LinearLayout实现自定义View
public class MyInflatView extends LinearLayout implements View.OnClickListener {    private TextView title;    private ImageView icon;    public MyInflatView(Context context) {        super(context);    }    public MyInflatView(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);        initView(context);    }    private void initView(Context context){    inflate(context,R.layout.custom_view,this);        title = (TextView) findViewById(R.id.title);        icon = (ImageView) findViewById(R.id.icon);        icon.setOnClickListener(this);    }    @Override    public void onClick(View v) {        if (v.equals(icon)){            Toast.makeText(getContext(),"点击了",Toast.LENGTH_SHORT).show();          }    }}XML: activity
<RelativeLayout 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="match_parent"    tools:context="com.example.fanyishuo.customitem.MainActivity">   <com.example.fanyishuo.customitem.View.MyView       android:layout_width="wrap_content"       android:layout_height="wrap_content"       app:text="小仙女"       app:textcolor="#fff0"       app:textsize="25sp"       android:layout_centerInParent="true" ></com.example.fanyishuo.customitem.View.MyView><com.example.fanyishuo.customitem.View.MyInflatView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_centerInParent="true"></com.example.fanyishuo.customitem.View.MyInflatView></RelativeLayout>
custom_view:
<?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">    <TextView        android:id="@+id/title"        android:text="@string/app_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <ImageView        android:id="@+id/icon"        android:src="@mipmap/ic_launcher"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

原创粉丝点击