如何在Android布局中正确对齐aChartEngine图表?

时间:2015-05-21 12:35:32

标签: android android-layout achartengine

我正在尝试编写一个代码,以便在Android布局中使用aChartengine绘制图形。我已成功绘制图形但我面临的问题是在特定ViewGroup内的布局中对齐Graph。

MyCode:

MainActivity.java

public class MainActivity extends ActionBarActivity {

List<double[]> xValues;
List<double[]> yValues;
List<double[]> yValues1;

GraphicalView view;
MultipleTemperatureChart multiple;
RelativeLayout rlt;

@Override
protected void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

    xValues = new ArrayList<double[]>();
    yValues = new ArrayList<double[]>();
    yValues1 = new ArrayList<double[]>();

    rlt = (RelativeLayout) findViewById(R.id.rlt);

    multiple = new MultipleTemperatureChart(this);

    int[] data = {143,144,145,137,156,146,158,139,144,143};

    double[] doubTime = new double[data.length];
    double[] doubNote = new double[data.length];
    double[] doubZero = new double[data.length];

    for(int i=0;i<data.length;i++){

        doubTime[i] = index;
        doubNote[i] = data[i];
        doubZero[i] = 0;

        index++;
    }

    xValues.add(doubTime);
    yValues.add(doubNote);
    yValues1.add(doubZero);     


    view =   multiple.execute(getApplicationContext(),xValues,yValues,yValues1);
    view.setBackgroundColor(Color.YELLOW);
    view.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 400));

    rlt.addView(view);

}


}

activity_main.xml中

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


        <RelativeLayout 
                android:layout_width="match_parent"
                android:layout_height="500dp"
                android:orientation="vertical"
                android:background="#33cc00"
                android:id="@+id/rltParams">

            <RelativeLayout 
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:id="@+id/rlt"/>

             <View 
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/rlt"
                    android:background="#cc4455"
                    android:id="@+id/view"/>

     </RelativeLayout>          

enter image description here

所以这些是我的问题的代码和截图。我想在绿色RelativeLayout 中绘制图表,并且红色视图需要低于图表布局。

请建议我在布局中正确绘制图形的一些解决方案或技巧。

1 个答案:

答案 0 :(得分:0)

如果你想把图表放在绿色布局中,试试这个:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


 <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="500dp"
    android:orientation="vertical"
    android:background="#33cc00"
    android:id="@+id/rltParams">

  <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/rlt"/>

 <View 
        android:layout_width="match_parent"
        android:layout_height="match_parent"                    
        android:background="#cc4455"
        android:id="@+id/view"/>

</RelativeLayout>  

只需删除以下行:

android:layout_below="@+id/rlt"

这是什么红色视图?

尝试这样的事情: (要查看红色视图,您需要在布局@ + id / rlt中放置一个高度。)

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="500dp"
    android:orientation="vertical"
    android:id="@+id/rltParams">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        android:background="#33cc00"
        android:id="@+id/rlt"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#cc4455"
        android:layout_below="@+id/rlt"
        android:id="@+id/view"/>

    </RelativeLayout>