2012-05-11 13 views
7

एक अजीब मिला है, तो LinearyLayout का आकार बदल नहीं जाएगा। मैंने इसे एक बहुत ही सरल उदाहरण में लाने में कामयाब रहा है। वास्तव में इसे समझ नहीं सकता है।इसकी ऊंचाई एक बार 0

मुझे लाइनरलाउट के अंदर एक लीनियरआउट मिला है। मैं एनीमेशन का उपयोग करके बच्चे का आकार बदलना चाहता हूं (जिसे मैं कभी-कभी इलाज कर रहा हूं)। मेरा लेआउट एक्सएमएल यहाँ है।

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

यदि आप इसे अभी चलाने के लिए थे, तो यह नीचे नहीं गिर जाएगा। बिलकुल। हालांकि, अगर आप बटन को लाइनरलायआउट में ले जाना चाहते थे, जिसे मैंने अवलोकन नाम दिया है और इसे चाइल्ड लीनियरलाउट के बाद रखा है, तो यह एक इलाज करता है! इस तरह ...

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

नहीं वास्तव में एक समाधान है, लेकिन जाने बटन वहां रहें और चले जाने के लिए इसकी दृश्यता निर्धारित करें। क्या यह इस तरह से काम करता है? – Urban

+0

फिर मैं इसका परीक्षण करने के लिए इसे क्लिक नहीं कर पाऊंगा। मैं वहां एक और वस्तु रखूंगा और कोशिश करूँगा। –

+0

नहीं, कोई पासा नहीं।यदि अगला भाई एनीमेशन का कारण बनने वाला बटन नहीं है, तो यह इसे निष्पादित नहीं करता है। कितना अजीब। यहां बहुत उलझन में है। –

उत्तर

2

साथ LinearLayout को android:layout_weight="1" जोड़ने की कोशिश मैं नहीं पता है कि आपका कोड एनीमेशन क्यों नहीं करना चाहता। लेकिन मैं थोड़ा बदलाव के साथ तय किया। मैंने जो किया वह एनीमेशन को पैरेंट लेआउट पर लागू करता है, न कि उस लेआउट को जो आप बदल रहे हैं। यहाँ xml का कोड है, मैं केवल माता-पिता लेआउट के लिए एक आईडी कहा:

<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 का कोड है:

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 को एक कोशिश कास्टिंग देने के लिए करना चाहते हैं:

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

अच्छा प्रयास, लेकिन कोई पासा नहीं। मैंने भी यह जानना चाहिए कि यह क्या होना चाहिए, इसलिए यह माता-पिता और माता-पिता में गुजरता है, गणना करने के बजाए, अभी भी कोई पासा नहीं है। :( –

2

सुनिश्चित नहीं हैं कि अगर यह मदद मिलेगी, लेकिन id="@+id/animation_subject"

+0

अब लेआउट को इसकी जगह का उपयोग करने में मदद मिलती है, इसलिए यह अपनी ऊंचाई उत्पन्न करता है। स्लाइडडाउन पर क्लिक करते समय हालांकि, यह 0 तक कूदता है, फिर नीचे स्लाइड करता है। लेकिन समस्या यह है कि इसकी ऊंचाई 0 प्रारंभ में नहीं है। –

+0

समस्या यह है कि 0 आकार xml में सेट है यह इंगित करता है कि आकार चाहिए स्वचालित रूप से बच्चों को देखने के आकार पर आधारित सेट करें। एक हैक के रूप में आप एक्सएमएल में 'चला गया' के लिए दृश्यता दृश्यता सेट कर सकते हैं और एनीमेशन सेट आकार के पहले समय प्रोग्रामेटिक रूप से 0 पर दृश्यता सेट कर सकते हैं और दृश्यता को 'View.VISIBLE' पर सेट कर सकते हैं। – Dmitry

संबंधित मुद्दे