无法在android中添加addView

无法在android中添加addView,android,android-graphview,Android,Android Graphview,我需要朋友的帮助 这是我的活动 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(save

我需要朋友的帮助

这是我的活动

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);
                }
            }
        }
    }
}
那么我的输出是:


您可以删除参数,以便

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));

这里的问题是,您正在将两个子视图的高度和宽度设置为linearlayout值

您应仅使用高度的一半来适应这两种情况;)

更新

你可以使用重量,比如:

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

graphview.setLayoutParams(params);
graphview1.setLayoutParams(params);
或者您可以从1级参数中获取高度:

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

然后将该高度乘以0.5,并将其用作视图布局中的高度参数,而不是换行内容。我想您应该添加另一个linearlayout并将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中添加linearlayout…

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

首先,我的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>
@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);

我同意,糟糕的否决票。拥有额外点数以恢复Androidverse的自然平衡。我没有否决投票,但我可能会调用
addView(view,0)
不会替换任何现有视图。@Sam您尝试过吗?我没有,但基于文档,当它没有说插入它将被替换。我以后再试试。谢谢你指出这一点。由于信息错误,我投了反对票。整数参数仅是索引为零的插入位置。。。更换不会发生。读了《我能实现这个朋友》的纪录片。你能再解释一下吗?这可能是一个简化的布局,但不需要线性布局和相对布局。您可以删除其中一个。您将获得所需的输出…但我不知道为什么它不适用于单一布局。。。
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
height /= 2;
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);