2012-05-11 21 views
7

奇妙なものがあります。私は非常に単純な例にそれを持ってきた。本当にそれを理解することはできません。LinearyLayoutは、高さが0になるとサイズ変更されません。

私はLinearLayout内にLinearLayoutを持っています。アニメーションを使って子のサイズを変更したいのですが(これは時には扱いがあります)。レイアウトXMLがここにあります。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> 
     <!-- Lots more controls will appear above --> 
     <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:id="@+id/animation_subject"> 
      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/hello" 
       android:gravity="center" /> 
     </LinearLayout> 
     <!-- Lots more controls will appear below --> 
    </LinearLayout> 

    <Button android:text="Slide it Down!" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="btnDoAnimDown_OnClick" /> 

</LinearLayout> 

ここでわかるように、ボタンがあります。ここにはそのボタンのコードがあります。

public void btnDoAnimDown_OnClick(View v) 
{ 
    View pnlSlider = findViewById(R.id.animation_subject); 
    pnlSlider.measure(1000, 1000); 
    DropDownAnim anim = new DropDownAnim(pnlSlider, pnlSlider.getMeasuredHeight(), true); 
    anim.setDuration(2000); 
    pnlSlider.startAnimation(anim); 

    return;  
} 

今すぐ実行すると、それは滑り落ちることはありません。まったく。しかし、ButtonをLinearLayoutに移動して、Overviewという名前のLinearLayoutをChild LinearLayoutの後に置くと、それは扱いになります!このように...

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> 
     <!-- Lots more controls will appear above --> 
     <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:id="@+id/animation_subject"> 
      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/hello" 
       android:gravity="center" /> 
     </LinearLayout> 
     <!-- Lots more controls will appear below --> 
     <Button android:text="Slide it Down!" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="btnDoAnimDown_OnClick" /> 
    </LinearLayout> 

</LinearLayout> 

これは、予想どおり正確にスライドします。明らかに、親レイアウトでは何かが起こっています... getMeasuredHeight()は正しい値を返しますので、アニメーションは実際には実行されません!

これはアニメーションクラスですが、私は何か愚かなことを逃していますか?おそらく午前!

import android.util.Log; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.Transformation; 

public class DropDownAnim extends Animation { 
    int targetHeight; 
    View view; 
    boolean down; 

    public DropDownAnim(View view, int targetHeight, boolean down) { 
     this.view = view; 
     this.targetHeight = targetHeight; 
     this.down = down; 
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     int newHeight; 
     if (down) { 
      newHeight = (int) (targetHeight * interpolatedTime); 
     } else { 
      newHeight = (int) (targetHeight * (1 - interpolatedTime)); 
     } 
     Log.d("new height", String.valueOf(newHeight)); 
     view.getLayoutParams().height = newHeight; 
     view.requestLayout(); 
    } 

    @Override 
    public void initialize(int width, int height, int parentWidth, 
      int parentHeight) { 
     super.initialize(width, height, ((View)view.getParent()).getWidth(), parentHeight); 
    } 

    @Override 
    public boolean willChangeBounds() { 
     return true; 
    } 
} 

どのようなヘルプも素晴らしいでしょう!

+0

ない正確に解決策が、聞かせて:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/animation_subject_parent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- Lots more controls will appear above --> <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:id="@+id/animation_subject"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:gravity="center" /> </LinearLayout> <!-- Lots more controls will appear below --> </LinearLayout> <Button android:text="Slide it Down!" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="btnDoAnimDown_OnClick" /> </LinearLayout> 

そしてここでは、ボタンclicクリックのコードです:ここに は、私が唯一の親のレイアウトにIDを追加、XMLのコードですボタンが表示されているかどうかを確認します。それはそのように機能しますか? – Urban

+0

これをクリックしてテストすることはできません。私はそこに別のオブジェクトを入れてそれを試みます。 –

+0

いいえ、サイコロはありません。次の兄弟がアニメーションの原因となるボタンでない場合は、それを実行しません。どのように奇妙です。ここでは非常に混乱しています。 –

答えて

2

LinearLayoutandroid:layout_weight="1"を追加しようと、私はしないでくださいあなたのコードがアニメーションをしたくない理由を知ってください。しかし、私は少し変更して固定しました。私がしたことは、あなたが変換しているレイアウトではなく、親レイアウトにアニメーションを適用することです。

public void btnDoAnimDown_OnClick(View v) 
{ 
    View pnlSlider = findViewById(R.id.animation_subject); 
    View parent = findViewById(R.id.animation_subject_parent); 

    pnlSlider.measure(1000, 1000); 
    DropDownAnim anim = new DropDownAnim(pnlSlider, pnlSlider.getMeasuredHeight(), true); 
    anim.setDuration(2000); 

    parent.startAnimation(anim); 

    return;  
} 
3

私はここに推測してみましょう。

最初のレイアウトでは、内側のレイアウトとボタンが外側の直線レイアウトを共有します。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> 
     <!-- Lots more controls will appear above --> 
     <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:id="@+id/animation_subject"> 
      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/hello" 
       android:gravity="center" /> 
     </LinearLayout> 
     <!-- Lots more controls will appear below --> 
    </LinearLayout> 

    <Button android:text="Slide it Down!" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="btnDoAnimDown_OnClick" /> 

</LinearLayout> 

これは、外部レイアウトをViewGroupにします。

しかし、アニメーションのinitializeメソッドでは、指定されたビューの親に対してキャストを行います。

Viewにキャストすると、1番目のレイアウトでは表示されません。

あなたの2番目のそれは、それはなぜその働きです。

だから私は、あなたがそれに代わりのViewGroupしようとする鋳造させていただきたいと思い:それは助ける

@Override 
    public void initialize(int width, int height, int parentWidth, 
      int parentHeight) { 
     super.initialize(width, height, ((ViewGroup)view.getParent()).getWidth(), parentHeight); 
    } 
+0

素敵な試みですが、ダイスはありません。それは、ダイスを計算するのではなく、parentWidthとparentHeightに渡します。:( –

2

わからない場合は、しかしid="@+id/animation_subject"

+0

これで、Layoutはスペースを使い果たしますので、それ自身の高さを生成します。しかし、それは0にジャンプし、次に滑り落ちます。しかし、高さの問題は最初は0ではありません。 –

+0

問題は、xmlに0サイズが設定されていることです。子ビューのサイズに基づいて自動的に設定されます。ハックとして、xmlのビューの可視性を 'gone'に、アニメーションセットのサイズをプログラムで0に設定し、可視性を' View.VISIBLE'に設定することができます – Dmitry

関連する問題