Android实现仿时间轴

移动开发 Android
在开发中,我们经常会遇到要求实现一些界面特效的,其中,时光轴的效果是最近新兴起来的,这种效果实现起来并不难,下面直接上效果和代码。

下面来看看时间轴的实现,效果如下图
   
    其实只不过是布局+动态生产TextView罢了,一开始选的是 FrameLayout,后来发现在处理单击事件的时候一个问题 ,例如:

  1. FrameLayout frameLayout= (FrameLayout) findViewById(R.id.frameLayout);   
  2.  
  3.  for(...){   
  4.  
  5.      frameLayout.add(tv1);   
  6.  
  7.     frameLayout.add(tv2);   
  8.  
  9.     //在这里直接处理单击事件肯定是不行的,tv1和tv2是重合在一起的   
  10.  
  11.  }   
  12.  
  13.      
  14.  
  15.      
  16.  
  17.  FrameLayout frameLayout= (FrameLayout) findViewById(R.id.frameLayout);   
  18.  
  19.  for(...){   
  20.  
  21.      tv1.setLayoutparams(....);   
  22.  
  23.     frameLayout.add(tv1);   
  24.  
  25.     frameLayout.add(tv2);   
  26.  
  27.    //在这里直接处理单击事件就可以了,不知道为什么?   
  28.  
  29.  }  

所以,直接改 成Linearlayout了,改成Linearlayout后,那些TextView的位置也好设置多了,下面是代码:


  1. package com.lliq.ui;    
  2. import android.app.Activity;    
  3.   
  4.  import android.os.Bundle;    
  5.   
  6.  import android.util.Log;    
  7.   
  8.  import android.view.View;    
  9.   
  10.  import android.view.View.OnClickListener;    
  11.   
  12. import android.view.Window;    
  13.   
  14.  import android.widget.FrameLayout;    
  15.   
  16.  import android.widget.LinearLayout;    
  17.   
  18.  import android.widget.TextView;    
  19.   
  20.     
  21.   
  22.  import com.lliq.R;    
  23.   
  24.     
  25.   
  26.  public class HistoryActivity extends Activity    
  27.   
  28. {    
  29.   
  30.     private final int space_year = 5;    
  31.   
  32.      private final int space_month = 5;    
  33.   
  34.     private String[] year = { "2010""2011""2012""2013" };    
  35.   
  36.     private String[][] month = { { "01""03""04""11" }, { "07" }, { "01""03""04""11" },    
  37.   
  38.             { "07" } };    
  39.   
  40.      
  41.   
  42.      boolean menu_falg = false;// 单击改变菜单图标    
  43.   
  44.       
  45.   
  46.     private TextView[] tv_year;    
  47.   
  48.     private TextView[] tv_month;    
  49.   
  50.      private TextView content;    
  51.   
  52.       
  53.   
  54.      @Override   
  55.   
  56.      protected void onCreate(Bundle savedInstanceState)    
  57.   
  58.      {    
  59.   
  60.         super.onCreate(savedInstanceState);    
  61.   
  62.         requestWindowFeature(Window.FEATURE_NO_TITLE);    
  63.   
  64.          setContentView(R.layout.iq_history);    
  65.   
  66.         initLayout();    
  67.   
  68.     }    
  69.   
  70.      
  71.   
  72.    private void initLayout()    
  73.   
  74.     {    
  75.   
  76.          LinearLayout btnback = (LinearLayout) findViewById(R.id.history_layouthome);    
  77.   
  78.          final TextView btnhome = (TextView) findViewById(R.id.history_btnhome);    
  79.   
  80.         btnback.setOnClickListener(new OnClickListener()    
  81.   
  82.        {    
  83.   
  84.            @Override   
  85.   
  86.             public void onClick(View arg0)    
  87.   
  88.             {    
  89.   
  90.                 menu_falg = !menu_falg;    
  91.   
  92.                btnhome.setBackgroundResource(menu_falg ? R.drawable.menuspread : R.drawable.menu_n);    
  93.   
  94.                MainActivity.handler.sendEmptyMessage(0);    
  95.   
  96.             }    
  97.   
  98.         });    
  99.   
  100.      
  101.   
  102.         content = (TextView) findViewById(R.id.content);    
  103.   
  104.         LinearLayout timelayout = (LinearLayout) findViewById(R.id.timelayout);    
  105.   
  106.         tv_year = new TextView[year.length];    
  107.   
  108.         for (int i = 0; i < year.length; i++)    
  109.   
  110.         {    
  111.   
  112.             tv_year[i] = new TextView(this);    
  113.   
  114.             tv_year[i].setPadding(    
  115.   
  116.                   10,    
  117.   
  118.                     i == 0 ? space_year : space_year    
  119.   
  120.                             * (13 - Integer.parseInt(month[i - 1][month[i - 1].length - 1])), 00);    
  121.   
  122.              tv_year[i].getPaint().setFakeBoldText(true);    
  123.   
  124.            tv_year[i].setTextSize(14);    
  125.   
  126.             tv_year[i].setTag(year[i]);    
  127.   
  128.              tv_year[i].setText(year[i] + "  --");    
  129.   
  130.             tv_year[i].setOnClickListener(new TimeLineClickListener(tv_year[i]));    
  131.   
  132.              timelayout.addView(tv_year[i]);    
  133.   
  134.             tv_month = new TextView[year.length];    
  135.   
  136.             for (int j = 0; j < month[i].length; j++)    
  137.   
  138.             {    
  139.   
  140.                 tv_month[i] = new TextView(this);    
  141.   
  142.                 if (j == 0)    
  143.   
  144.                 {    
  145.   
  146.                     tv_month[i].setPadding(20, space_month * Integer.parseInt(month[i][j]), 00);    
  147.   
  148.                  } else   
  149.   
  150.                 {    
  151.   
  152.                     tv_month[i].setPadding(20, space_month    
  153.   
  154.                              * (Integer.parseInt(month[i][j]) - Integer.parseInt(month[i][j - 1])),    
  155.   
  156.                             00);    
  157.   
  158.                 }    
  159.   
  160.                  tv_month[i].setTextSize(12);    
  161.   
  162.                  tv_month[i].setText(month[i][j] + "月   --");    
  163.   
  164.                 tv_month[i].setTag(year[i] + "-" + month[i][j]);    
  165.   
  166.                  tv_month[i].setOnClickListener(new TimeLineClickListener(tv_month[i]));    
  167.   
  168.                timelayout.addView(tv_month[i]);    
  169.   
  170.             }    
  171.   
  172.         }    
  173.   
  174.      
  175.   
  176.     }    
  177.   
  178.      
  179.   
  180.     class TimeLineClickListener implements OnClickListener    
  181.   
  182.      {    
  183.   
  184.      
  185.   
  186.         TimeLineClickListener(View v)    
  187.   
  188.          {    
  189.   
  190.        }    
  191.   
  192.      
  193.   
  194.         @Override   
  195.   
  196.          public void onClick(View v)    
  197.   
  198.          {    
  199.   
  200.              content.setText(v.getTag().toString());    
  201.   
  202.         }    
  203.   
  204.     }    
  205.   
  206.      
  207.   
  208.  }   

 

责任编辑:张叶青 来源: 开源社区
相关推荐

2015-11-09 15:42:28

2021-02-20 09:36:11

数据大数据数据可视化

2021-04-16 06:48:39

Windows10操作系统微软

2023-05-31 18:58:16

得物人事系统时间轴

2014-08-15 15:01:29

时间轴

2020-07-26 12:01:08

PythonPython 3.8开发

2017-12-20 21:12:04

2021-05-25 10:52:33

Windows10操作系统微软

2015-03-31 13:36:37

编程时间轴插件详细说明

2018-04-08 18:29:22

Windows 10组织工具时间轴

2021-04-15 07:14:06

Windows10操作系统21H2

2018-05-14 10:56:25

APPiOS开发代码

2021-04-15 12:45:05

Windows 10Windows操作系统

2023-02-24 09:54:54

开源React

2012-12-27 10:51:14

Android开发iPhone时间轮

2023-05-04 08:09:44

Windows 11微软

2023-11-18 10:19:52

微软Windows

2011-09-19 16:40:40

2014-08-18 14:18:07

Android桌面悬浮

2015-10-23 13:36:22

点赞
收藏

51CTO技术栈公众号