2017-04-25 126 views
0

我正在嘗試用兩個單獨的圖形數據視圖編寫應用程序 - 一個是X-Y圖形。另一個是X vs T圖。我想要兩個垂直排列,X-Y圖形在上面。我寫了一個擴展到管理繪圖的View類,這不是問題。Android佈局異常 - 實際佈局不符合Android Studio外觀。

我最初使用的RelativeLayout:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:custom="http://schemas.android.com/apk/res-auto" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#CCCCFF" 
    android:orientation="vertical" 
    android:gravity="center_horizontal"> 

    <RelativeLayout 
     android:id="@+id/button_bar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal"> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="clearCanvas" 
      android:text="@string/btn1Text" 
      style="@android:style/Widget.Button" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="startPlot" 
      android:text="@string/btn2Text" 
      android:layout_toRightOf="@id/button1" 
      style="@android:style/Widget.Button" /> 

    </RelativeLayout> 

    <co.android.bmd.chaoswithtime.CanvasView 
     android:id="@+id/plot_canvas" 
     android:layout_width="1000px" 
     android:layout_height="1000px" 
     android:textColor="#FFFFFF" /> 

    <co.android.bmd.chaoswithtime.CanvasView 
     android:id="@+id/trend_canvas" 
     android:textColor="#FFFFFF" 
     tools:layout_below="@id/plot_canvas" 
     android:layout_height="500px" 
     android:layout_width="1000px" /> 

</RelativeLayout 

根據Android Studio中所示的佈局,這應該得到所需的結果: enter image description here XT視圖覆蓋在XY視圖:

enter image description here

我已經嘗試了許多變體來獲得對齊排列,但唯一的方法是爲XT視圖添加一個很大的頂部邊距 - 否非常滿意。

我可以使用LinearLayout得到我想要的,但無法弄清爲什麼Relativelayout不起作用(以及爲什麼Android Studio中的預覽與來自同一個包的模擬結果之間存在差異(仿真結果在實際設備上居然出現爲好。)

任何人都可以想出一個解釋?

+0

爲什麼工具:layout_below而不是android:layout_below? – samgak

+0

這就是我從AS中的佈局包得到的 - 它已經修復了繪圖順序。但現在這兩個圖形面板是左對齊的,而不是中央。 – user1815293

+0

使用android:layout_centerHorizo​​ntal =「true」 – samgak

回答

0

您已通過的寬度和像素高度,這意味着它的內容,當你運行你的代碼,只需要1000像素在屏幕上

更好的是,你應該使用LinearLayout作爲您的自定義視圖的父級,並使用重量和weightSum。並將match_parent的寬度和高度傳遞給LinearLayout

你可以試試下面的xml。這可以幫助你。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:custom="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#CCCCFF" 
    android:gravity="center_horizontal" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:id="@+id/button_bar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal"> 

     <Button 
      android:id="@+id/button1" 
      style="@android:style/Widget.Button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="clearCanvas" 
      android:text="@string/btn1Text" /> 

     <Button 
      android:id="@+id/button2" 
      style="@android:style/Widget.Button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/button1" 
      android:onClick="startPlot" 
      android:text="@string/btn2Text" /> 

    </RelativeLayout> 

    <LinearLayout 
     android:layout_below="@+id/button_bar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:weightSum="3"> 

     <co.android.bmd.chaoswithtime.CanvasView 
      android:id="@+id/plot_canvas" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="2" 
      android:textColor="#FFFFFF" /> 

     <co.android.bmd.chaoswithtime.CanvasView 
      android:id="@+id/trend_canvas" 
      android:layout_height="0dp" 
      android:layout_weight="2" 
      android:textColor="#FFFFFF" 
      tools:layout_below="@id/plot_canvas"1 
     android:layout_width="match_parent" /> 
    </LinearLayout> 
</RelativeLayout 
+0

在這個階段,我使用一個固定的佈局與一個目標 - 一旦我得到基本問題排序。我會考慮添加代碼來應對不同的目標。 – user1815293

+0

@ user1815293請檢查上面的XML並嘗試這一點。 –