2013-04-29 147 views
1

您好我正在android.I創建一個自定義視圖有一個LinerarLayout我添加自定義views.I設法添加一個自定義視圖編程,但如果我添加另一個它是沒有得到添加到layout.I不知道哪裏會出錯。我的主要活動。無法以編程方式將自定義視圖添加到佈局

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     LinearLayout container = (LinearLayout) findViewById(R.id.container); 
     final MyAnimationView animView = new MyAnimationView(this); 
     container.addView(animView); 
     final MyAnimationView animView1 = new MyAnimationView(this);   
     container.addView(animView1);  
    } 

和我的自定義視圖類

public class MyAnimationView extends TextView implements ValueAnimator.AnimatorUpdateListener { 

public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>(); 
AnimatorSet animation = null; 
private float mDensity; 

public MyAnimationView(Context context) { 
    super(context);   
    mDensity = getContext().getResources().getDisplayMetrics().density; 
    ShapeHolder ball0 = addBall(75f, 400f);    

} 
public MyAnimationView(Context context,float x,float y) { 
    super(context);  
    mDensity = getContext().getResources().getDisplayMetrics().density; 
    ShapeHolder ball0 = addBall(105f, 400f);   



} 

private void createAnimation() { 
    if (animation == null) {     
     ObjectAnimator anim0=ObjectAnimator.ofFloat(balls.get(0),"y",getHeight(),500f).setDuration(500); 
     animation = new AnimatorSet(); 
     animation.playTogether(anim0);    
     anim0.addUpdateListener(this); 


    } 
} 

private ShapeHolder addBall(float x, float y) { 
    OvalShape circle = new OvalShape(); 
    circle.resize(100f * mDensity, 100f * mDensity); 
    ShapeDrawable drawable = new ShapeDrawable(circle); 
    ShapeHolder shapeHolder = new ShapeHolder(drawable); 
    shapeHolder.setX(x - 25f); 
    shapeHolder.setY(y - 25f); 
    int red = (int)(100 + Math.random() * 155); 
    int green = (int)(100 + Math.random() * 155); 
    int blue = (int)(100 + Math.random() * 155); 
    int color = 0xff000000 | red << 16 | green << 8 | blue; 
    Paint paint = drawable.getPaint(); //new Paint(Paint.ANTI_ALIAS_FLAG); 
    int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4; 
    RadialGradient gradient = new RadialGradient(37.5f, 12.5f, 
      50f, color, darkColor, Shader.TileMode.CLAMP); 
    paint.setShader(gradient); 
    shapeHolder.setPaint(paint); 
    balls.add(shapeHolder); 
    return shapeHolder; 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    for (int i = 0; i < balls.size(); ++i) { 
     ShapeHolder shapeHolder = balls.get(i); 
     canvas.save(); 
     canvas.translate(shapeHolder.getX(), shapeHolder.getY()); 
     shapeHolder.getShape().draw(canvas); 
     canvas.restore(); 
    } 
} 

public void startAnimation() { 
    createAnimation(); 
    animation.start(); 
} 

public void onAnimationUpdate(ValueAnimator animation) { 
    invalidate(); 
} 
} 

編輯:這是我的線性佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools"  
android:id="@+id/container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context=".MainActivity" > 

<Button 
    android:id="@+id/startButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 

任何幫助將不勝感激。

+0

請發佈xml佈局。我認爲你必須做一些LayoutParams來適應linearLayout中的兩個視圖。 – Opiatefuchs 2013-04-29 13:59:26

+0

嘗試更改'LinearLayout'的'定位' – 2013-04-29 14:00:03

+0

@Opiatefuchs嗨,好友我已經發布了我的xml – vinoth 2013-04-29 14:03:32

回答

0

不,它會被添加,但你看不到它。因爲您的第一個視圖將佔用屏幕的全部空間。而第二個視圖將添加到您的第一個視圖的下方,這樣它就會隱藏在設備的可見部分中。我也遇到了同樣的問題。我用兩種佈局解決了這個問題。有關我的解決方案,請參閱here

我想這是爲什麼我提供這個解決方案,如果不是請讓我知道..

+0

所以我如何解決這個好友 – vinoth 2013-04-29 14:11:26

+0

對不起朋友。看到我更新的答案。只要引用它,我會在一小時內回覆。現在我正在離開辦公室。 – Gunaseelan 2013-04-29 14:30:48

+0

現在很多好友我設法得到它。 – vinoth 2013-04-30 04:56:25

2

只是一個假設,鴕鳥政策知道,如果它的問題。嘗試在你的主要做到這一點:

LinearLayout container = (LinearLayout) findViewById(R.id.container); 

    final MyAnimationView animView = new MyAnimationView(this); 

    animView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams. 
           WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f)); 

    container.addView(animView); 

    final MyAnimationView animView1 = new MyAnimationView(this); 

    animView1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams. 
           WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f)); 

    container.addView(animView1); 

它工作?

相關問題