2011-09-06 52 views
0

我最近問了一個關於如何在視圖頂部添加視圖的問題,之後詢問我意識到我需要在繼續進一步之前向我的應用添加更好的佈局。我的Android佈局應該是什麼樣子?

我在讀Android Layout Tricks,但注意到它專門用於文本視圖和圖像視圖。我正在尋找與兩個自定義視圖。所以我決定在油漆上畫一個快速圖像,希望更清楚地表明我想要做的事情。


這是我多麼希望我的佈局分割的意見。 :

enter image description here


這是它會如何看待與得出的意見。顯然,紫色和藍色邊界將是背景顏色(灰色)。上面的數據只是顯示用相應顏色繪製的圖的y軸截距。 (所以有將是對彼此頂部繪製多個圖形視圖)

enter image description here

所以我的問題是,會是什麼我的主要內容視圖是什麼樣子?我認爲它會有一個線性佈局,但我對這些佈局頗爲陌生。

編輯

使用TextViews我能拿出類似的東西使用下面的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <TextView 
      android:text="Data Placeholder" 
      android:background="#733674" 
      android:textSize="15pt" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="20"/> 

     <TextView 
      android:text="Graph Placeholder" 
      android:background="#374F82" 
      android:textSize="15pt" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="80"/> 

    </LinearLayout> 

</LinearLayout> 

因此,其實仍然是唯一的問題是,我應該使用TextViews ?在我的活動意義是我能夠添加我的自定義視圖這些TextViews是哪裏?或者我應該將自定義視圖添加到XML中?

即,

<DataView 
     android:text="Data Placeholder" 
     android:background="#733674" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="20"/> 

    <GraphView 
     android:text="Graph Placeholder" 
     android:background="#374F82" 
     android:textSize="15pt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="80"/> 

我的自定義視圖是用Java編寫的,我不知道如何讓佈局知道視圖的位置,如果我這樣做。

+0

您是否在尋找XML代碼或...? –

+0

@Michell Bak正確,我在尋找Xml這個:)我想我應該澄清一點。 – StartingGroovy

+1

不完全確定您對自定義數據和圖形視圖的期望,但如果您只是尋找代碼來拆分兩個視圖(大約30/70,如圖所示),則可以使用垂直LinearLayout中的layout_weight參數。 –

回答

1

試試這個:http://developer.android.com/resources/tutorials/views/hello-linearlayout.html

它有一些可能幫助你的問候layout_weight爲米歇爾樸在評論中提及了非常有用的信息。

而這裏的頁面爲你好瀏覽: http://developer.android.com/resources/tutorials/views/index.html

不粗魯,但它會是更好的給您細讀這些和自己學習的XML。這樣你就可以真正理解它,並能夠更好地在以後重新創建它。

起初我對所有我不明白的代碼(包括xml文件)感到不知所措,但稍加練習就會變得非常容易 - 只需要很長時間。

我很困惑的主要是什麼樣的視圖放在 佈局。在這些例子中,他們使用的TextView或ImageView的,但我是一個 自定義視圖

嗯,你的「自定義數據視圖」,您將使用與android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:background="#BA4AAB"一個的LinearLayout(見http://www.colorpicker.com/

然後爲您的自定義圖形視圖,我會用: android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="4" android:background="#7D4ABA"

注意layout_weight和背景值,我把是種猜測,你可能必須調整他們一些墊上你想要什麼。

我提到將是一個較大的LinearLayout裏面有android:orientation="vertical"

然後在頂部的數據這兩個linearlayouts,你可以使用4個文本視圖,並在代碼中,你會使用的setText(... )對這些文本的意見把你的數據的採集。

的XML中textview1,你會增加android:id="@+id/textview1"然後在代碼添加TextView textview1 = (TextView)findviewbyId(R.id.textview1);然後textview1.setText(myString);

對於底部的曲線圖,你會用2次圖的基礎,並設置android:layout_wi dth和android:layout_height以適合您使用dip,dp或px單位的任何內容。

爲你畫的,我相信你將不得不使用Canvas類有一個位圖,並呼籲canvas.drawLine(...)線(見http://developer.android.com/reference/android/graphics/Canvas.html

+0

我很困惑的主要問題是在佈局中放置什麼樣的視圖。在他們使用TextView或ImageView的例子中,我的是一個自定義視圖。 – StartingGroovy

+0

我想你錯過了我在主要問題中的編輯。我爲佈局創建了基本的XML。我只是困惑如何將我的'數據視圖'和'圖形視圖'納入它。 (我已經用Java代碼創建了我的數據和圖形視圖,但我不確定如何將它們添加到佈局) – StartingGroovy

+0

如果您有頂部的線性佈局和底部的線性佈局,那麼您在java代碼中創建你的數據視圖,然後你會得到你的頂級線性佈局(如linLayoutTop),然後做linLayoutTop.addView(dataView);這是否回答你的問題?如果沒有,我想我不是100%確定你在這個問題上所要求的。 – Jakar

相關問題