2011-09-02 249 views
0

我在Android和主視圖中有一個圖形視圖(自定義視圖)。根據用戶的偏好,我想在我的主視圖中添加1-5個圖形視圖,但我不太確定如何。 (我使用純Java而不是xml)。我在讀,我可能不得不使用相對佈局或其他東西來堆疊視圖。如何在Android視圖中添加自定義視圖?

的任何意見或建議,歡迎

+0

這是否意味着有5個圖表例如當你的看法應該是滾動的?順便說一下,這可以通過在畫布上繪圖來實現。 – Lumis

+0

@Lumis不,我想在上圖中繪製圖形。例如,讓我說我的第一個圖是一個圓,我的下一個切線函數圖。我想要在圓形圖上顯示切線圖。 (因爲我想從每個圖形視圖中獲得信息,所以我不想僅將它們繪製在頂部)。希望清除它有點:) – StartingGroovy

+0

在自定義視圖中,您可以繪製無限數量的圖表,而不管它們是否具有透明度。程序如何知道用戶什麼時候繪製下一個圖形? – Lumis

回答

3

在你的活動,你可能有這樣的事情對你的onCreate的開頭()方法:

setContentView(R.layout.main); 

您的main.xml文件中,你可能有一個元素是某種佈局。我現在將假設LinearLayout,但它與所有類型的工作方式類似。你需要得到這個佈局的參考,並做到這一點,它必須有一個ID。因此,如果該佈局沒有這樣的事情在裏面,你需要添加它:

android:id="@+id/myMainLayout" 

然後)回到你的java你所謂的setContentView(之後的某個時間,你可以找到類似的參考佈局這樣的:

LinearLayout myLayout = (LinearLayout)findViewById(R.id.myMainLayout); 

一旦你有你的佈局的參考,你可以添加一些您的圖形視圖,它是這樣的:

myLayout.addView(graph1); 
myLayout.addView(graph2); 
//etc... 

如果你想跳過XML佈局所有T一起允許你在java中進行佈局。要做到這一點,將是這樣的:

LinearLayout myLayout = new LinearLayout(YourActivity.this); 
myLayout.addView(graph1); 
myLayout.addView(graph2); 
setContentView(myLayout); 

請注意,您只能調用的setContentView()一次,所以你將需要某種形式的佈局傳遞給它,如果你想添加超過1查看。

編輯:

我從來沒有具體試過,但我想你可以把從構造addView()在您的自定義視圖:

public CustomView() { 
    this.addView(anotherView); 
} 

你有你的佈局自定義視圖太?

+0

如果我正確地閱讀你的文章,你正在設置佈局,並在活動中添加視圖。有沒有辦法在View中而不是Activity中做到這一點? – StartingGroovy

+0

目前我有兩個擴展View的Java類。我還沒有爲其中的任何一個設置佈局。在另一個說明中,我似乎沒有爲我的視圖添加'addView()'方法。我問是否有可能在視圖中執行此操作的原因是因爲我有一種方法可以抓取數據並根據數據繪製新圖並添加或刪除以前的圖 – StartingGroovy

+0

addView()是一種方法ViewGroup,你必須擴展它來使用它。 – FoamyGuy

1

以下是自定義視圖的示例,如圖所示。一個人需要有一個的LinearLayout在某個地方擁有ID爲@ +圖形的ID/LL和大小的佈局:

public class RootActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    int[] graphData = {3,5,2,7,4,8,1,5,9}; 

    LinearLayout ll = (LinearLayout) findViewById(R.id.ll); 
    GraphView graphView = new GraphView(this); 
    ll.addView(graphView); 


    //call this method with every new set of data 
    graphView.drawGraph(graphData); 


} 


class GraphView extends View{ 
int[] graphData; 
Paint graphPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
int screenH; 
int screenW; 
int colW; 
int colH; 
int columnCount; 

public GraphView(Context context) { 
    super(context); 

    graphPaint.setColor(Color.MAGENTA); 
    graphPaint.setStyle(Style.FILL); 
} 


@Override  
public void onSizeChanged (int w, int h, int oldw, int oldh) {   
    super.onSizeChanged(w, h, oldw, oldh);   
    screenW = w;   
    screenH = h; 
} 


public void drawGraph(int[] graphData){ 
    this.graphData = graphData; 
    columnCount = graphData.length; 
    invalidate(); 
} 


@Override  
public void onDraw(Canvas canvas) {   
    super.onDraw(canvas); 

    colW = (screenW - 10)/columnCount; 
    int graphStep = 20; 
    int columnSpace = 5; 

    canvas.drawText("GRAPH", 10, 10, graphPaint); 

    for (int i= 0 ; i < columnCount; i++){ 
     //draw columns from bottom up 
     canvas.drawRect(
       new Rect(
       i * colW + 5, 
       screenH - 5 - (graphData[i] * graphStep), 
       i * colW + 5 + colW - columnSpace, 
       screenH - 5  
       ), 
       graphPaint); 
    } 



} 
+0

我繼續前進,並在類似主題上發佈了另一個問題,如果您有興趣查看它:http://stackoverflow.com/questions/7326535/what-should-my-android- layout-look-like – StartingGroovy

+0

如果我沒有弄錯,這是否只使用一個視圖,而是在該視圖上繪製多個圖形? – StartingGroovy

+0

是的,只有一個視圖應該是。 – Lumis

相關問題