• 重绘各种View的最简单方式。。。。。----------------------Android


    今晚搞android自定义View,遇到了一个小问题。。爬了半天帖子,才爬到。。现在在这记录下来

    自定义view,有两种方式。。。。。。

    1.先继承一个view---然后通过后台的draw方法给控件绘制前台效果。这个方法。对于我来说,有点操蛋。。我不太喜欢这个方式

    2:继承view,然后用各种资源文件,layout文件,还有动画。。堆积成一个个控件,后台代码只要很少很少的东西就行 了。。。为什么要这样?因为这样写代码更爽。

    第一步:写一个类,继承一个view-这里千万注意构造函数----不然会出错

    package com.example.mycontrols;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    
    import com.example.controlshelpers.R;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * Created by li on 2016/3/14.
     */
    public class Mylistview extends ListView {
    
    
        //构造函数
        public Mylistview(Context context,AttributeSet paramAttributeSet) {
            super(context,paramAttributeSet);
            SimpleAdapter adapter = new SimpleAdapter(context, getData(), R.layout.mylistviewitem,
                    new String[]{"title", "info", "img"},
                    new int[]{R.id.title, R.id.info, R.id.img});
            this.setAdapter(adapter);
        }
    
        private List<Map<String, Object>> getData() {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", "G1");
            map.put("info", "google 1");
            map.put("img", R.drawable.image1);
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("title", "G2");
            map.put("info", "google 2");
            map.put("img", R.drawable.image2);
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("title", "G3");
            map.put("info", "google 3");
            map.put("img", R.drawable.image3);
            list.add(map);
    
            return list;
    
        }
    
    
    }

    注意构造函数里的那个layout文件哦。。。这个文件在这里呢

    <?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">
    
        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5px" />
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FFFFFFFF"
                android:textSize="22px" />
    
            <TextView
                android:id="@+id/info"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FFFFFFFF"
                android:textSize="13px" />
        </LinearLayout>
    </LinearLayout>

    好了。。。。这也,这个控件就完成了。。虽然布局什么的都很丑。。但是,原理出来了

    最后一步,在另外一个工程里引用,是一个activity哦

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:id="@+id/mainwindow"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:context=".MainActivity">
    
        <ImageView
            android:id="@+id/ivMore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@drawable/backgroundimage" />
    
        <RelativeLayout
            android:id="@+id/listParent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true">
    
            <com.example.mycontrols.Mylistview
                android:id="@+id/listMore"
                android:layout_width="256px"
                android:layout_height="fill_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true" />
        </RelativeLayout>
    </RelativeLayout>
  • 相关阅读:
    java多线程--线程和线程池
    java多线程--锁学习
    vue项目中使用iconfont
    组件封装-无数据组件
    添加自定义字体
    时间格式化(自定义格式)
    深度克隆方法
    LazyMan面试题
    lodash.throttle实现节流
    第6章:关系数据库理论(考研重点)
  • 原文地址:https://www.cnblogs.com/xiaoleye/p/5277858.html
Copyright © 2020-2023  润新知