欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

教你3分钟了解Android 简易时间轴的实现方法

程序员文章站 2022-10-10 13:02:30
一.有段时间没更了,因为一直在思索,应该写点什么,真的是无比纠结。这一回,就给大家分享一款简便好用的,小编自制的土晾时间轴。 附上xml预览图: 效果图 注...

一.有段时间没更了,因为一直在思索,应该写点什么,真的是无比纠结。这一回,就给大家分享一款简便好用的,小编自制的土晾时间轴。

附上xml预览图:

教你3分钟了解Android 简易时间轴的实现方法

效果图 注:小黄鸭不是效果哈,是为了保护个人隐私p上去的:

教你3分钟了解Android 简易时间轴的实现方法

1.新建一个自定义控件:

public class workexcview extends linearlayout {
private textview dataleft;
private textview dataright;
private textview company;
private textview job;
private textview jobasses;
private textview xitong;
private textview zitian;
private linearlayout jobcontentcontainer;
private textview jobcontent;
private linearlayout commentcontainer;
private linearlayout niancontentcontainer;
private textview niancontent;
private linearlayout base;
private linearlayout yuexingcontainer;
private textview yuecontent;
private linearlayout nianxingcontainer;
private textview yuemorecontent;
private textview nianmorecontent;
private string company_id;

public workexcview(context context) {
  this(context,null);
}

public workexcview(context context, attributeset attrs) {
  super(context, attrs);
  init();
}

private void init() {
  view inflate = view.inflate(getcontext(), r.layout.activity_four_workexcview, this);
  dataleft = (textview) inflate.findviewbyid(r.id.workexcview_data_left);
  dataright= (textview) inflate.findviewbyid(r.id.workexcview_data_right);
  company = (textview) inflate.findviewbyid(r.id.workexcview_company);
  job = (textview) inflate.findviewbyid(r.id.workexcview_job);
  jobasses = (textview) inflate.findviewbyid(r.id.workexcview__job_asses);
  xitong = (textview) inflate.findviewbyid(r.id.workexcview_xitong);
  zitian = (textview) inflate.findviewbyid(r.id.workexcview_zitian);
  jobcontent = (textview) inflate.findviewbyid(r.id.workexcview_jobcontent);
  niancontent = (textview) inflate.findviewbyid(r.id.workexcview_niancontent);
  jobcontentcontainer = (linearlayout)inflate.findviewbyid(r.id.workexcview_jobcontent_container);
  commentcontainer = (linearlayout)inflate.findviewbyid(r.id.workexcview__comment_container);
  niancontentcontainer= (linearlayout)inflate.findviewbyid(r.id.workexcview__niancontent_container);
  base = (linearlayout)inflate.findviewbyid(r.id.workexcview__base);
  yuexingcontainer = (linearlayout)inflate.findviewbyid(r.id.workexcview_yuexing_container);
  yuecontent = (textview)inflate.findviewbyid(r.id.workexcview_yuecontent);
  nianxingcontainer = (linearlayout)inflate.findviewbyid(r.id.workexcview_nianxing_container);
  yuemorecontent = (textview)inflate.findviewbyid(r.id.workexcview_yuecontentmore);
  nianmorecontent = (textview)inflate.findviewbyid(r.id.workexcview_niancontentmore);
}

public void initdata(final fourbean.jobsbean jobsbean) {
   //赋值代码
}

xml文件:

    <?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"
android:id="@+id/workexcview__base"
android:paddingleft="8dp">

<relativelayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

  <imageview
    android:id="@+id/workexcview_landian"
    android:layout_width="15dp"
    android:layout_height="15dp"
    android:layout_centervertical="true"
    android:src="@mipmap/landian" />

  <textview
    android:id="@+id/workexcview_data_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centervertical="true"
    android:layout_marginleft="3dp"
    android:layout_torightof="@+id/workexcview_landian"
    android:text="2016.03"
    android:textcolor="@color/text"
    android:textsize="15sp" />

  <textview
    android:id="@+id/workexcview__data_zhong"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_torightof="@id/workexcview_data_left"
    android:text=" —— "
    android:textcolor="@color/text" />

  <textview
    android:id="@+id/workexcview_data_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centervertical="true"
    android:layout_torightof="@+id/workexcview__data_zhong"
    android:text="2016.03"
    android:textcolor="@color/text"
    android:textsize="15sp" />

  <textview
    android:id="@+id/workexcview_zitian"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignparentright="true"
    android:background="@drawable/four_select"
    android:padding="4dp"
    android:paddingright="6dp"
    android:text="自填"
    android:textcolor="@color/red"
    android:textsize="12sp"
    android:visibility="gone" />


  <!--    <imageview
        android:id="@+id/workexcview_xitong"
        android:layout_width="40dp"
        android:layout_height="20dp"
        android:layout_alignparentright="true"
        android:layout_centervertical="true"
        android:paddingright="10dp"
        android:src="@mipmap/xitong"
        android:visibility="gone" />-->
  <textview
    android:id="@+id/workexcview_xitong"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignparentright="true"
    android:background="@drawable/textview_shape"
    android:padding="4dp"
    android:paddingright="6dp"
    android:text="系统"
    android:textcolor="@color/holo_blue_light"
    android:textsize="12sp"
    android:visibility="gone" />

  <relativelayout
    android:id="@+id/four_setting_workexc_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignparentright="true"
    android:visibility="gone">

    <textview
      android:id="@+id/four_setting_workexc_logo"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignparentright="true"
      android:layout_centervertical="true"
      android:text="编辑"
      android:textcolor="@android:color/holo_blue_light"
      android:textsize="16sp" />

    <imageview
      android:layout_width="20dp"
      android:layout_height="20dp"
      android:layout_centervertical="true"
      android:layout_toleftof="@+id/four_setting_workexc_logo"
      android:src="@drawable/xierijidianji3x" />
  </relativelayout>
</relativelayout>

<linearlayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">

  <relativelayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <textview
      android:layout_width="1dp"
      android:layout_height="match_parent"
      android:layout_marginleft="7dp"
      android:background="@color/black" />

  </relativelayout>

  <linearlayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <relativelayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="8dp">

      <textview
        android:id="@+id/workexcview_company"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centervertical="true"
        android:text="某某公司"
        android:textcolor="@color/text"
        android:textsize="14sp" />

      <textview
        android:id="@+id/workexcview_company_zhong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centervertical="true"
        android:layout_torightof="@id/workexcview_company"
        android:text=" / "
        android:textcolor="@color/text"
        android:textsize="14sp" />

      <textview
        android:id="@+id/workexcview_job"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centervertical="true"
        android:layout_torightof="@id/workexcview_company_zhong"
        android:text="web前段工程师"
        android:textcolor="@color/text"
        android:textsize="14sp" />

    </relativelayout>

    <linearlayout
      android:id="@+id/workexcview_jobcontent_container"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginleft="5dp"
      android:orientation="vertical"
      android:padding="8dp">

      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="工作描述:"
        android:textcolor="@color/text" />

      <textview
        android:id="@+id/workexcview_jobcontent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margintop="3dp"
        android:text="巴拉巴拉" />

    </linearlayout>
    <linearlayout
      android:id="@+id/workexcview__yuecontent_container"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginleft="5dp"
      android:orientation="vertical"
      android:padding="8dp"
      android:visibility="visible">

      <relativelayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <textview
          android:id="@+id/workexcview_yuecontent_logo"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centervertical="true"
          android:text="最新月评价:"
          android:textcolor="@color/text" />
        <linearlayout
          android:layout_centervertical="true"
          android:id="@+id/workexcview_yuexing_container"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:layout_torightof="@+id/workexcview_yuecontent_logo"
          >

        </linearlayout>
        <textview
          android:layout_alignparentright="true"
          android:id="@+id/workexcview_yuecontentmore"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_margintop="3dp"
          android:gravity="center"
          android:padding="2dp"
          android:layout_centervertical="true"
          android:background="@drawable/textview_shape"
          android:text="更多"
          android:textcolor="@color/blue_title" />
      </relativelayout>


      <textview
        android:id="@+id/workexcview_yuecontent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margintop="3dp"
        android:text="无"
        android:textcolor="@color/gray" />


    </linearlayout>
    <linearlayout
      android:id="@+id/workexcview__niancontent_container"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginleft="5dp"
      android:orientation="vertical"
      android:padding="8dp"
      android:visibility="visible">

      <relativelayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <textview
          android:id="@+id/workexcview_niancontent_logo"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centervertical="true"
          android:text="最新年评价:"
          android:textcolor="@color/text" />
        <linearlayout
          android:layout_centervertical="true"
          android:id="@+id/workexcview_nianxing_container"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:layout_torightof="@id/workexcview_niancontent_logo"
          >

        </linearlayout>
        <textview
          android:id="@+id/workexcview_niancontentmore"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_margintop="3dp"
          android:padding="2dp"
          android:layout_centervertical="true"
          android:background="@drawable/textview_shape"
          android:text="更多"
          android:layout_alignparentright="true"
          android:textcolor="@color/blue_title" />
      </relativelayout>


      <textview
        android:id="@+id/workexcview_niancontent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margintop="3dp"
        android:text="无"
        android:textcolor="@color/gray" />


    </linearlayout>

    <linearlayout
      android:id="@+id/workexcview__comment_container"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="5dp"

      android:orientation="vertical"
      android:padding="8dp"
      android:visibility="visible">

      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="离职评价:"
        android:textcolor="@color/text" />

      <textview
        android:id="@+id/workexcview__job_asses"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margintop="3dp"
        android:text=""
        android:textcolor="@color/gray" />
    </linearlayout>


  </linearlayout>
 </linearlayout>
</linearlayout>      

2.使用方式:其实很简单,直接for循环new出来赋值就行啦,因为本身就是一个自定义控件~

  fourworkexccontainer.removeallviews();
  for (int i = 0; i < jobs.size(); i++) {
    fourbean.jobsbean jobsbean = jobs.get(i);
    workexcview workexcview = new workexcview(getcontext());
    workexcview.initdata(jobsbean);
    fourworkexccontainer.addview(workexcview);
  } 

3.适合自己的才是最好的,大家可以根据自己的情况进行修改,也可以查阅更多的资料,以上只是一种实现的方式,随手分享。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。