2014-12-01 78 views
0

我試圖在Android中創建自己的自定義條形圖,但我遇到了問題。我見過的圖形庫不提供我需要的靈活性和自定義。無論如何,我處於開始階段,並且有一個非常基本的佈局,基本上是放置「酒吧」的地方。 xml低於Android自定義條形圖佈局不添加

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="250dp" 
    android:layout_gravity="center" 
    android:layout_margin="0dp" 
    android:duplicateParentState="true" 
    android:orientation="horizontal"> 
    <FrameLayout 
     android:layout_width="150dp" 
     android:layout_height="match_parent" 
     android:background="@drawable/sectioned_borders" 
     > 

     <FrameLayout 
      android:id="@+id/frame1" 
      android:layout_width="match_parent" 
      android:layout_height="238dp" 
      android:layout_gravity="bottom" > 

     </FrameLayout> 
    </FrameLayout> 
    <FrameLayout 
     android:layout_width="150dp" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="10dp" 
     android:background="@drawable/sectioned_borders" 
     > 

     <FrameLayout 
      android:id="@+id/frame2" 
      android:layout_width="match_parent" 
      android:layout_height="238dp" 
      android:layout_gravity="bottom" > 

     </FrameLayout> 
    </FrameLayout> 

    <FrameLayout 
     android:layout_width="150dp" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="10dp" 
     android:background="@drawable/sectioned_borders" 
     > 

     <FrameLayout 
      android:id="@+id/frame3" 
      android:layout_width="match_parent" 
      android:layout_height="238dp" 
      android:layout_gravity="bottom" > 

     </FrameLayout> 
    </FrameLayout> 

</LinearLayout> 

每個FrameLayout是一個部分,我將把這些條放在佈局中。現在,我想動態放置這些條,因爲它可以根據存在的條數而變化。所以我把一個基本的課程和活動放在一起,以便在本節中得到一些東西。我有以下類包含「欄」信息:

package com.example.graph.resources; 

import android.content.Context; 
import android.graphics.Color; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.FrameLayout; 
import android.widget.TableLayout; 

public class Bar { 

    private final static int MAX_PERCENT = 100; 
    private final static int MIN_PERCENT = 0; 

    private int percent = 0; 
    private String buttonText = ""; 
    private String barID = ""; 
    private Drive barDrive; 
    public TableLayout barLayout; 

    public Bar(Context context, int percent, String buttonText, String barID, int width) { 
     this.percent = (percent >= 0 && percent <= 100) ? percent : 0; 
     this.buttonText = (buttonText != null) ? buttonText : ""; 
     this.barID = (buttonText != null) ? buttonText : ""; 

     barDrive = new Drive(); 

     // Create bar view 
     TableLayout tableLayout = new TableLayout(context); 
     TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(46, TableLayout.LayoutParams.MATCH_PARENT); 
     tableLayout.setStretchAllColumns(true); 
     tableLayout.setLayoutParams(tableParams); 

     FrameLayout frameLayout = new FrameLayout(tableLayout.getContext()); 
     FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, (int)(tableLayout.getHeight() * 0.9)); 
     frameParams.leftMargin = 3; 
     frameLayout.setLayoutParams(frameParams); 

     // Create Views (bars) 
     View viewUnderlay = new View(frameLayout.getContext()); 
     LayoutParams viewUnderlayParams = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
     viewUnderlay.setLayoutParams(viewUnderlayParams); 

     View viewOverlay = new View(frameLayout.getContext()); 
     LayoutParams viewOverlayParams = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);  
     viewOverlay.setLayoutParams(viewOverlayParams); 
     viewOverlay.setBackgroundColor(Color.BLUE); 


     // combine views 
     frameLayout.addView(viewUnderlay); 
     frameLayout.addView(viewOverlay); 
     tableLayout.addView(frameLayout); 
     this.barLayout = tableLayout;  
    } 

    // Maybe more advantageous to make this a separate class... 
    private class Drive { 

     private int driveNumber = 0; 
     private int value = 0; 

     public Drive() { 
     // TODO Default Drive Info 
     } 

     private Drive(int driveNumber, int value) { 
     // TODO Setup Drive Info 
     } 


    } 

} 

正如你所看到的。 「Bar」類只是設置一些成員數據,然後我在這裏添加「bar」佈局。所以在我的主要活動課上,我只是嘗試填充第一幀。我的主類則是:

package com.example.bargraphtest2; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewTreeObserver; 
import android.view.ViewTreeObserver.OnGlobalLayoutListener; 
import android.widget.FrameLayout; 
import android.widget.LinearLayout; 

import com.example.bargraphtest2.R; 
import com.example.graph.resources.Bar; 

public class MainActivity extends Activity { 

    LinearLayout la; 
    FrameLayout frame1; 
    Context ctx; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.dynamic_graph_base_layout); 

     frame1 = (FrameLayout)this.findViewById(R.id.frame1); 
     ctx = this; 
     ViewTreeObserver vto = frame1.getViewTreeObserver(); 
     vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
      frame1.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
      //test with frame 1 
      int testPadding = 3; 
      int barCount = 3; 

      int width = (frame1.getWidth() - ((barCount + 1) * testPadding))/barCount; 

      for(int i = 1; i <= barCount; i++){ 
       Bar bar = new Bar(ctx,i * 10, "1", "bar" + i, width); 
       frame1.addView(bar.barLayout); 
      } 
      frame1.refreshDrawableState(); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
} 

所以,我嘗試運行這一點,我只是把我的一般格式(XML格式)。我沒有得到(3)酒吧。當我調試下面的代碼時,我會在創建條形圖(佈局)時逐步瀏覽,但layoutparams中的寬度和高度未正確設置。我在這裏看到了負值,這對MATCH_PARENT是正確的,但對於實際寬度不適用。我看到計算寬度的合理值。所以......我在這裏做錯了什麼?任何建議/幫助將不勝感激。非常感謝你!

+0

因爲這張貼是昨晚發佈的,所以我會把它撞上來,以確保它不會被掩埋。 – 2014-12-02 12:21:28

回答

0

好吧,我想我明白了。因此,這裏需要考慮兩件事情......首先,在定義LayoutParams時,確保基於父容器創建參數,而不是實際創建的容器/佈局。例如,在上面的例子中,當創建一個新的TableLayout時,我將把LayoutParams定義爲FrameLayout.LayoutParams。另一件事,我認爲是所有問題的原因是上下文......我並沒有在整個過程中使用相同的上下文,而是在搞亂我的佈局。因此,請使用您的活動/應用程序生成的上下文。我揹着每個孩子的語境,我想這就是搞砸了事情。無論如何,我能夠在想要的框架上獲得一個藍色條,這是我現在想要做的。希望這可以幫助某人。