绘制可修改的图表

时间:2014-12-31 21:57:47

标签: android xml android-layout android-activity android-studio

我正在尝试绘制如下图所示的图表,并想知道是否有更有效的方法在XML中执行此操作。以下是我未完成的代码,但我想听听别人的建议和例子。我希望能够在稍后阶段复制和修改代码。我希望图表在所有设备上都能正确显示(即合适的边距,填充等)。非常感谢所有有用的回复。

diagram

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

    <RelativeLayout
        android:id="@+id/colored_bar"
        android:layout_width="10dp"
        android:layout_height="40dp"
        android:background="@color/central"/>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:background="@color/grey"
        android:layout_weight=".25" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="1"
            android:textColor="@color/black"/>
    </RelativeLayout>

    <View
        android:id="@+id/car_separator0"
        android:layout_width="1dp"
        android:layout_height="50dp"
        android:background="@color/black" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:background="@color/grey"
        android:layout_weight=".25" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="2"
            android:textColor="@color/black"/>
    </RelativeLayout>

    <View
        android:id="@+id/car_separator1"
        android:layout_width="1dp"
        android:layout_height="50dp"
        android:background="@color/black" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:background="@color/grey"
        android:layout_weight=".25" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="3"
            android:textColor="@color/black"/>

        <RelativeLayout
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_weight=".25"
            android:padding="1.5dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="15dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:padding="1.5dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>



        <RelativeLayout
            android:layout_width="15dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:padding="1.5dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="15dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:padding="1.5dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="15dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:padding="1.5dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="15dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:padding="1.5dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>
    </RelativeLayout>

    <View
        android:id="@+id/car_separator2"
        android:layout_width="1dp"
        android:layout_height="50dp"
        android:background="@color/black" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:background="@color/grey"
        android:layout_weight=".25" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="4"
            android:textColor="@color/black"/>

        <RelativeLayout
            android:layout_width="15dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_alignParentLeft="true"
            android:padding="1.5dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="15dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_centerHorizontal="true"
            android:padding="1.5dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="15dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_alignParentRight="true"
            android:layout_marginRight="5dp"
            android:padding="1.5dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="15dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true"
            android:padding="1.5dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="15dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:padding="1.5dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="15dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginRight="5dp"
            android:padding="1.5dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="10dp"
        android:layout_height="40dp"
        android:background="@color/central"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:2)

我强烈建议不要在xml中绘制它。相反,这是自定义视图的完美用途。绘图应该相当容易。例如,小盒子可以简单地,快速地在for循环中绘制:

for (int y = 0; y < canvasWidth; y+=yStride) {
  canvas.drawRect(littleRect, paint);
}

真的,手工绘制这个最大的痛苦就是触摸IO,但即使这样也不是太糟糕。

相关问题