天天看點

Android 時間軸的實作

Android 時間軸的實作

直接上代碼

xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

        <View
            android:id="@+id/iew_1"
            android:layout_width="1dp"
            android:layout_height="0dp"
            android:layout_marginLeft="41dp"
            android:background="@color/light_gray" />

        <RelativeLayout
            android:id="@+id/rl_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="32dp"
                    android:background="@mipmap/yuan" />
        </RelativeLayout>

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/iew_1"
            android:paddingBottom="20dp"
            android:layout_marginLeft="55dp"
            android:lineSpacingMultiplier="1.5"
            android:textColor="#A6A6A6" />

        <View
            android:id="@+id/v_line"
            android:layout_width="1dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/rl_title"
            android:layout_marginLeft="41dp"
            android:background="@color/light_gray" />
</RelativeLayout>      
兩個View實作的是與圓圈上下的兩根豎線,       
ImageView實作就是圓圈      
activity代碼:      
package app.view.home.insertclass;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import app.R;

/**
 * Created by sch on 2016/9/27.
 */
@ContentView(R.layout.insert_class_catalog_group_content)
public class ICCGroupContentAdapter extends BaseAdapter {
    Context context;
    private LayoutInflater inflater;
    @ViewInject(R.id.title)
    private TextView textView;
    @ViewInject(R.id.v_line)
    private View line;
    private View view;
    private String[] title = {
            "隻手上飛機是克服了收款方sdfsafjaslfjlasdjlfjlsadjfladslfjldsajfldslfjldsfslafjlsjfpwelfmdslfjsflaslfjlasf式",
            "隻手上飛機是克服了收款方sadfkasfnl式隻",
            "回頭啊,莫回頭!曾經誰都曾回望自己的過去,但我從不曾想回頭,是女孩也該向前進,找到更好的你自己,",
            " FB上面有一個視訊,名字叫做Alwayslikeagirl。視訊裡采訪了不同年齡段的人,有小男孩,也有女青年",
            "有一些姑娘,既能夠畫着淡妝,踩着小高跟,花枝招展,巧笑嫣然地出現在人前;也能夠穿着舊T恤和短褲,連一口水都沒時間喝卻一句怨言都沒有。"
    };

    public ICCGroupContentAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return title.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        inflater = LayoutInflater.from(parent.getContext());
        View root = x.view().inject(this, inflater, parent);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) line.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.rl_title);
        params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.title);

        line.setLayoutParams(params);
        textView.setText(title[position]);
        return root;
    }
}
      
代碼解釋:      
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) line.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.rl_title);
        params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.title);

        line.setLayoutParams(params);      
這一部分代碼是設定圓圈下面的豎線的高度,使其自适應高度