2

我需要帮助的朋友。

这是我的活动

public class MainActivity extends Activity {

    float values[] = { 700, 400, 100, 500, 600 };
    float values1[] = { 70, 40, 10, 50 };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout lv1 = (LinearLayout) findViewById(R.id.linear);

        values = calculateData(values);
        values1 = calculateData(values1);
        MyGraphview graphview = new MyGraphview(this, values);
        graphview.setLayoutParams(new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        lv1.addView(graphview, 0);
        MyGraphview1 graphview1 = new MyGraphview1(this, values1);
        graphview1.setLayoutParams(new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        lv1.addView(graphview1, 0);

    }

    private float[] calculateData(float[] data) {
        float total = 0;
        for (int i = 0; i < data.length; i++) {
            total += data[i];
        }
        for (int i = 0; i < data.length; i++) {
            data[i] = 360 * (data[i] / total);
        }
        return data;
    }

    public class MyGraphview extends View {
        private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        private float[] value_degree;
        RectF rectf = new RectF(150, 150, 350, 350);

        float temp = 0;

        public MyGraphview(Context context, float[] values) {

            super(context);

            value_degree = new float[values.length];
            for (int i = 0; i < values.length; i++) {
                value_degree[i] = values[i];
            }
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Random r;
            for (int i = 0; i < value_degree.length; i++) {
                if (i == 0) {
                    r = new Random();
                    int color = Color.argb(100, r.nextInt(256), r.nextInt(256),
                            r.nextInt(256));
                    paint.setColor(color);
                    canvas.drawArc(rectf, 0, value_degree[i], true, paint);

                } else {
                    temp += value_degree[i - 1];
                    r = new Random();
                    int color = Color.argb(255, r.nextInt(256), r.nextInt(256),
                            r.nextInt(256));
                    paint.setColor(color);
                    canvas.drawArc(rectf, temp, value_degree[i], true, paint);
                }
            }
        }
    }

    public class MyGraphview1 extends View {
        private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        private float[] value_degree;
        RectF rectf = new RectF(170, 370, 330, 530);

        float temp = 0;

        public MyGraphview1(Context context, float[] values) {

            super(context);

            value_degree = new float[values.length];
            for (int i = 0; i < values.length; i++) {
                value_degree[i] = values[i];
            }
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Random r;
            for (int i = 0; i < value_degree.length; i++) {
                if (i == 0) {
                    r = new Random();
                    int color = Color.argb(100, r.nextInt(256), r.nextInt(256),
                            r.nextInt(256));
                    paint.setColor(color);
                    canvas.drawArc(rectf, 0, value_degree[i], true, paint);

                } else {
                    temp += value_degree[i - 1];
                    r = new Random();
                    int color = Color.argb(255, r.nextInt(256), r.nextInt(256),
                            r.nextInt(256));
                    paint.setColor(color);
                    canvas.drawArc(rectf, temp, value_degree[i], true, paint);
                }
            }
        }
    }
}

这是我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linear"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </LinearLayout>

</RelativeLayout>

这是我的结果

错误的输出

但预期结果是

预期产出

我的代码朋友有什么问题。请帮我。提前致谢。

如果我删除参数,那就是

添加视图(视图);并删除索引

        MyGraphview graphview = new MyGraphview(this, values);
//      graphview.setLayoutParams(new LinearLayout.LayoutParams(
//              LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        lv1.addView(graphview);
        MyGraphview1 graphview1 = new MyGraphview1(this, values1);
//      graphview1.setLayoutParams(new LinearLayout.LayoutParams(
//              LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        lv1.addView(graphview1);

然后我的输出是:

在此处输入图像描述

4

4 回答 4

2

这里的问题是您将两个子视图的高度和宽度设置为线性布局值。

你应该只使用一半的高度来适应两者;)

更新

好吧,您可以使用权重,如下所示:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
            params.weight = 1.0f;

graphview.setLayoutParams(params);
graphview1.setLayoutParams(params);

或者您可以从 lv1 参数中获取高度:

LinearLayour.LayoutParams lvparams = lv1.getLayoutParams();
int lvHeight = lvParams.height;

然后将该高度乘以 0.5 并将其用作视图布局中的高度参数,而不是 WRAP_CONTENT

于 2013-03-08T19:06:13.713 回答
2

您可以删除参数,这样就可以了

lv1.addView(graphview);
lv1.addView(graphview1);

在你的情况下你不需要它。

更新:现在将每个权重添加到 1。

graphview.setLayoutParams(new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,1));
graphview1.setLayoutParams(new LinearLayout.LayoutParams(
              LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,1));
于 2013-03-08T19:00:19.967 回答
1

我想您应该添加另一个线性布局并将您的 addview 添加到其中,例如...

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LinearLayout lv1 = (LinearLayout) findViewById(R.id.linear);
    LinearLayout lv2 = (LinearLayout) findViewById(R.id.linear1);
    values = calculateData(values);
    values1 = calculateData(values1);
    MyGraphview graphview = new MyGraphview(this, values);
    graphview.setLayoutParams(new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    lv1.addView(graphview, 0);
    MyGraphview1 graphview1 = new MyGraphview1(this, values1);
    graphview1.setLayoutParams(new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    lv2.addView(graphview1, 0);

}

并且不要忘记在您的 xml 中添加线性布局...

于 2013-03-08T19:32:24.610 回答
0

非常感谢所有试图解决我问题的朋友。我找到了以下修改的解决方案。我认为这个答案将对寻找这个问题的朋友的解决方案有所帮助。因为现在我感觉到我们老板的最后一刻压力。

首先,我的 xml 会根据 @Karan 的建议进行更改。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linear"        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear1"        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </LinearLayout>

</LinearLayout>

然后我重写onMeasureMyGraphview 和 MyGraphview1 类中的方法,将高度设置为@Merlins 建议。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            this.setMeasuredDimension(width, height);
        }

宽度和高度是

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
height /= 2;

然后改变onCreate方法

LinearLayout lv1 = (LinearLayout) findViewById(R.id.linear);
LinearLayout lv2 = (LinearLayout) findViewById(R.id.linear1);
values = calculateData(values);
values1 = calculateData(values1);

MyGraphview graphview = new MyGraphview(this, values);
MyGraphview1 graphview1 = new MyGraphview1(this, values1);

lv1.addView(graphview);
lv2.addView(graphview1);
于 2013-03-08T20:14:55.700 回答