Android 在水平时间线上设置文本

Android 在水平时间线上设置文本,android,xml,android-custom-view,annotatedtimeline,Android,Xml,Android Custom View,Annotatedtimeline,我正在尝试实现水平时间线。因此,我已经编写了设计水平线的代码,但我能够确定如何在水平线的上方和下方编写文本 还有一件事,我不想使用任何其他图书馆 我试图通过自定义视图来解决这个问题,因为这里的人已经被建议过了,但是被打动了 timeline_segment.xml <?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android">

我正在尝试实现水平时间线。因此,我已经编写了设计水平线的代码,但我能够确定如何在水平线的上方和下方编写文本

还有一件事,我不想使用任何其他图书馆

我试图通过自定义视图来解决这个问题,因为这里的人已经被建议过了,但是被打动了

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>

决定回答,因为评论框有点限制。以下是我的评论:


您将需要一个自定义视图来实现这一点。您可以组合现成视图,也可以完全自定义视图

如果您选择合成视图,则可以从将图像逐级分解开始。在最高级别上,它是一个带有“
TimelineCell
”的水平布局(或您选择的任何名称)

TimelineCell
基本上是一个垂直的
LinearLayout
,具有右对齐的
TextView
、绘制直线的
视图和另一个中心对齐的
TextView

然后,您可以通过编程方式创建它们,并将它们添加到父级水平
LinearLayout

但是,如果您选择完全定制,则必须处理所有组件的测量、布局和绘图,包括线条上方和下方的文本


看看android上自定义视图的详细介绍

要实现这一点,您需要自定义视图。你可以合成现成的视图,也可以完全定制。你能给我提供一个链接,我可以在那里检查代码。时间线我想它需要动态创建,这意味着每次你都会添加一个新的项目符号,对吗?在这种情况下,您可以在每次添加新视图时以编程方式创建视图。这意味着您必须创建一个LinearLayout方向垂直,其中有2个文本视图和1个line.ohk。谢谢,老兄,我想我找到解决办法了。没问题……很高兴能至少给他们一个提示。。我已尝试创建自定义视图,但无法获取。你能帮忙吗。我已经添加了java和xml文件hey@AnkurKhandelwal,很高兴你修复了它。
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);
    }

}