【问题标题】:Android Custom Bar Graph Layouts Not AddingAndroid自定义条形图布局未添加
【发布时间】:2014-12-01 22:27:04
【问题描述】:

我正在尝试在 Android 中创建自己的自定义条形图,但遇到了问题。我见过的图形库没有提供我需要的灵活性和自定义。无论如何,我正处于开始阶段,并且有一个非常基本的布局,基本上是放置“条”的位置的占位符。 xml如下

<?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 都是我将在布局中放置条形的部分。现在,我想动态放置条形图,因为它可以根据存在多少条形图而变化。所以我把一个基本的课程和活动放在一起,只是为了在这个部分画出一些东西。我有以下包含“Bar”信息的类:

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
      }


   }

}

如您所见。 “栏”类只是设置了一些成员数据,然后我尝试在此处添加“栏”布局。因此,在我的主要活动课程中,我只是尝试填充第一帧。我的主要课程是:

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 但不适用于实际宽度。我看到计算宽度的合法值。所以......我在这里做错了什么?任何建议/帮助将不胜感激。非常感谢!

【问题讨论】:

  • 因为这是昨晚发布的,所以我会把它顶起来以确保它不会被埋没。

标签: java android user-interface graph


【解决方案1】:

好吧,我想我想通了。所以,这里有两件事要考虑......首先,在定义 LayoutParams 时,请确保您是基于父容器而不是您实际创建的容器/布局来创建参数。例如,在上面的示例中,当创建一个新的 TableLayout 时,我将 LayoutParams 定义为 FrameLayout.LayoutParams。另一件事,我认为是你所有问题的原因是上下文......我没有在整个过程中使用相同的上下文,它弄乱了我的布局。因此,请使用您的活动/应用程序生成的上下文。我带着每个孩子的背景,我认为这就是把事情搞砸的原因。无论如何,我能够在所需的框架上获得一个蓝色条,这就是我现在想要做的。希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多