今天就跟大家聊聊有關(guān)Android開(kāi)發(fā)中怎么自定義時(shí)間軸,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
我們提供的服務(wù)有:成都網(wǎng)站制作、網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、延慶ssl等。為成百上千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的延慶網(wǎng)站制作公司
具體內(nèi)容如下
時(shí)間軸效果,實(shí)際上非常簡(jiǎn)單,就是listView中一個(gè)又一個(gè)的條目而已….大家可以只關(guān)注一個(gè)條目。
首先展示一個(gè)條目的布局效果
<?xml version="1.0" encoding="utf-8"?>
接下來(lái)看一下自定義的TimeView如何書寫
package com.example.time; import java.util.Random; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Rect; import android.util.AttributeSet; import android.view.View; public class TimeView extends View { private Random random; private String time; private Rect mBounds = new Rect(); private int rgb; public TimeView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); } public TimeView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public TimeView(Context context) { super(context); initView(); } private void initView() { random = new Random(); //定義顏色---這里純粹為了好玩--大家定義的時(shí)候可以在自定義控件外邊定義,將顏色傳遞進(jìn)來(lái) rgb = Color.rgb(100+random.nextInt(155), 100+random.nextInt(155), random.nextInt(100+155)); } public void setTime(String time) { this.time = time; invalidate(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = new Paint(); paint.setColor(rgb); paint.setAntiAlias(true); paint.setStyle(Style.FILL_AND_STROKE); //先繪制圓 canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, paint); paint = new Paint(); paint.setColor(Color.BLACK); paint.setTextSize(30); paint.getTextBounds(time, 0, time.length(), mBounds); float textWidth = mBounds.width(); float textHeight = mBounds.height(); //再繪制文字 canvas.drawText(time, getWidth() / 2 - textWidth / 2, getHeight() / 2 + textHeight / 2, paint); } }
看一下Activity中的代碼–就是一個(gè)ListView的效果展示
public class MainActivity extends Activity { private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); initData(); listView.setAdapter(new MyBaseAdapter()); } class MyBaseAdapter extends BaseAdapter { @Override public int getCount() { return dataList.size(); } @Override public Object getItem(int arg0) { return dataList.get(arg0); } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { View view = View.inflate(MainActivity.this, R.layout.item, null); TextView tv_content = (TextView) view.findViewById(R.id.tv_content); TextView tv_time = (TextView) view.findViewById(R.id.tv_time); TimeView timeView = (TimeView) view.findViewById(R.id.timeView); timeView.setTime(dataList.get(arg0).getTime()); tv_content.setText(dataList.get(arg0).getContent()); tv_time.setText(dataList.get(arg0).getTime()); return view; } } ArrayListdataList = new ArrayList (); private void initData() { for (int i = 0; i < 20; i++) { dataList.add(new DataBean("哈哈哈哈" + i, "25/10")); } } }
看完上述內(nèi)容,你們對(duì)Android開(kāi)發(fā)中怎么自定義時(shí)間軸有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。