Could not add addView in android

孤人 提交于 2019-12-11 03:44:55

问题


I need help friends.

Here is my activity

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

And this is my 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>

And this is my result

But Expected result is

What is wrong in my code friends. please help me. Thanks in advance.

if I remove the params so it would be

addView(view); and remove Index

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

Then my output is:


回答1:


You can remove the params so it would be

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

you don't need it in your case.

Update: now add the weight to 1 each.

graphview.setLayoutParams(new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,1));
graphview1.setLayoutParams(new LinearLayout.LayoutParams(
              LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,1));



回答2:


The problem here is that you are setting the height and width of both child views to the linearlayout values.

You should use only half the height to fit both in ;)

UPDATE

Well you can use weights, like so:

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

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

Or you could get the height from the lv1 params:

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

Then multiply that height by 0.5 and use it as your height argument in you view layout instead of WRAP_CONTENT




回答3:


i guess you should add another linearlayout and add your addview to it for eg...

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

}

and dont forget to add linearlayout in your xml...




回答4:


Thanks lot who all are trying to solve my problem friends. I have found solution with following modifications. I think the answer will be helpful to someone who searching solution for this problem friends. Because now I feel the last minute presser from our boss.

First my xml changes as per @Karan suggestions.

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

then I override onMeasure method in both MyGraphview and MyGraphview1 classes as folling to set height as @Merlins suggestions.

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

width and height are

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

then changes in onCreate method

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


来源:https://stackoverflow.com/questions/15301271/could-not-add-addview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!