在水平时间线中设置文本

时间:2016-12-04 11:28:37

标签: android xml android-custom-view annotatedtimeline

我正在尝试实现水平时间轴。所以我编写了设计水平线的代码,但我能够弄清楚如何在线的上方和下方写文字。

还有一件事我不想使用任何其他库。

Target ImageT

我试图通过自定义视图解决它,因为这里有人建议但是被击中了。

timeline_segment.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
    android:weightSum="1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
       android:padding="3dp"
        android:textAlignment="textEnd"
        android:text="Top"
        android:id="@+id/top_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <TextView
            android:layout_gravity="center"
            android:layout_weight="0.5"
            android:background="@color/alphabet_a"
            android:layout_width="wrap_content"
            android:layout_height="2dp" />

        <ImageView
            android:background="@drawable/circle1"
            android:layout_width="15dp"
            android:layout_height="15dp" />

        <TextView
            android:layout_weight="0.5"
            android:layout_gravity="center"
            android:background="@color/alphabet_a"
            android:layout_width="wrap_content"
            android:layout_height="2dp" />

    </LinearLayout>


    <TextView
        android:padding="3dp"
        android:gravity="center"
        android:text="bottom"
        android:id="@+id/bottom_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
</merge>

timeline_segment.java

public class timeline_segement extends LinearLayout {

    View rootView;
    TextView upperText;
    TextView startLine;
    TextView endLine;
    ImageView circleView;
    TextView bottomText;

    public timeline_segement(Context context) {
        super(context);
        init(context);
    }

    public timeline_segement(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public timeline_segement(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context) {

      rootView=inflate(context, R.layout.timeline_segment, this );

      upperText=(TextView) rootView.findViewById(R.id.top_data);
      bottomText=(TextView) rootView.findViewById(R.id.bottom_data);

      upperText.setText("Top");
      bottomText.setText("Bottom");

    }

    public  void setUpperText(String string)
    {
        upperText.setText(string);
    }

    public void setBottomText(String string)
    {
        bottomText.setText(string);
    }

}

0 个答案:

没有答案