view 时间轴

来源:互联网 发布:表白二维码生成器软件 编辑:程序博客网 时间:2024/06/05 20:47
//主布局
<ListView    android:id="@+id/listView"    android:divider="@null"    android:layout_width="match_parent"    android:layout_height="match_parent"></ListView>

//item布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="75dp" android:paddingLeft="30dp" android:orientation="vertical" > <View android:layout_width="1dp" android:layout_height="20dp" android:layout_gravity="center_horizontal" android:background="#ff0000"> </View> <test.bwie.com.lianxi3.view.CircleTimeView android:id="@+id/circleTimeView" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center_horizontal" android:src="@mipmap/ic_launcher" /> <View android:layout_width="1dp" android:layout_height="20dp" android:layout_gravity="center_horizontal" android:background="#ff0000"> </View> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="75dp" android:paddingLeft="60dp" android:orientation="vertical" > <TextView android:id="@+id/text1" android:text="标题" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" > </TextView> <TextView android:id="@+id/text2" android:text="描述" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" > </TextView> </LinearLayout></LinearLayout>


//自定义view

public class CircleTimeView extends View { private int measuredHeight; private int measuredWidth; private int color=Color.GREEN; private String text=""; public CircleTimeView(Context context) { super(context); } public CircleTimeView(Context context, AttributeSet attrs) { super(context, attrs); } public CircleTimeView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); measuredHeight = getMeasuredHeight(); measuredWidth = getMeasuredWidth(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint=new Paint(); paint.setColor(color); paint.setStyle(Paint.Style.FILL); paint.setAntiAlias(true); paint.setStrokeWidth(2); canvas.drawCircle(measuredWidth/2,measuredHeight/2,measuredWidth/2-1, paint); paint.setTextSize(20); paint.setColor(Color.BLACK); Rect rect=new Rect(); paint.getTextBounds(text,0,text.length(),rect); canvas.drawText(text,measuredWidth/2-rect.width()/2,measuredHeight/2+rect.height()/2,paint); } public void setColor(int color){ this.color=color; } public void setText(String text){ this.text=text; }}



public class MainActivity extends Activity {    private SlideUnlockView slideUnlockView;    private ImageView imageView;    private Vibrator vibrator;    private ListView listView;    private ArrayList<InfoData> list;    Random random=new Random();    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        listView = (ListView) findViewById(R.id.listView);        list = new ArrayList<>();        for (int i = 0; i < 30; i++) {            list.add(new InfoData("张三"+i,"条目"+i));        }        listView.setAdapter(new MyBaseAdapter());    }//适配器 class MyBaseAdapter extends BaseAdapter{        @Override        public int getCount() {            return list.size();        }        @Override        public Object getItem(int position) {            return list.get(position);        }        @Override        public long getItemId(int position) {            return position;        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            View view=View.inflate(MainActivity.this,R.layout.time_layout,null);            CircleTimeView circleTimeView= (CircleTimeView) view.findViewById(R.id.circleTimeView);            TextView textView= (TextView) view.findViewById(R.id.text1);            TextView textView2= (TextView) view.findViewById(R.id.text2);            textView.setText(list.get(position).getTitle());            textView2.setText(list.get(position).getDes());            long time=System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();            SimpleDateFormat format=new SimpleDateFormat("mm:ss");            Date d1=new Date(time);            String t1=format.format(d1);            circleTimeView.setText(t1);            circleTimeView.setColor(Color.rgb(100+random.nextInt(155),100+random.nextInt(155),100+random.nextInt(155)));            return view;        }    }
}









 
0 0
原创粉丝点击