2012-12-05 20 views
11

मेरे कोड में मेरे पास एक एक्सएमएल फ़ाइल है जिसमें कई बच्चे लेआउट हैं। और मैं बस डिवाइस की स्क्रीन चौड़ाई के अनुसार इन बाल लेआउट की चौड़ाई प्रोग्रामेटिक रूप से सेट करना चाहता हूं। अब तक मैंने लेआउट पैरा, सेटपरम और अन्य विधियों का प्रयास किया था लेकिन यह लेआउट आईडी प्राप्त करने पर शून्य सूचक अपवाद दिखाता है।एंड्रॉइड में प्रोग्रामेटिक रूप से बाल लेआउट चौड़ाई सेट करें?

मेरे माता-पिता लेआउट एक रिश्तेदार लेआउट, और 1 बच्चा लेआउट एक रेखीय लेआउट ("इस लेआउट के लिए मैं चौड़ाई बदलने की जरूरत है")

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/parentlayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:id="@+id/childlayout" 
     android:layout_width="250dip" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:background="#153746"> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:background="@drawable/sub_title" 
      android:gravity="center_vertical" > 
      <TextView 
       android:id="@+id/menu_header_label" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textStyle="bold" 
       android:text="asd" 
       android:textColor="#ffffff" 
       android:layout_marginLeft="10dip" /> 

      <ImageView 
       android:id="@+id/menu_header" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" /> 
     </LinearLayout> 
     <ListView 
      android:id="@+id/menu_listview" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:divider="@android:color/darker_gray" 
      android:dividerHeight="1dip" 
      android:scrollingCache="false" /> 
    </LinearLayout> 

    <FrameLayout 
     android:id="@+id/overlay" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
    </FrameLayout> 

</LinearLayout> 

जावा कोड है: -

 public class SlideMenu extends LinearLayout { 

    // keys for saving/restoring instance state 
    public final static String KEY_MENUSHOWN = "menuWasShown"; 
    private final static String KEY_STATUSBARHEIGHT = "statusBarHeight"; 
    private final static String KEY_SUPERSTATE = "superState"; 

// Display display = ((WindowManager) getWindowToken()).getDefaultDisplay(); 
// int width = display.getWidth(); // deprecated 
// int height = display.getHeight(); // deprecated 
// float layWidth = (width*70)/100; 
// 
//// LinearLayout view= (LinearLayout)findViewById(R.id.childlayout); 
//// LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams(); 
//// params.width = 130; 
//// view.setLayoutParams(params); 
// view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant)); 

    public static class SlideMenuItem { 
     public int id; 
     public Drawable icon; 
     public String label; 
    } 

यहां मेरा कोड स्निपेट है और मैंने उस भाग पर टिप्पणी की जो मैंने अभी तक कोशिश की है और कोई अच्छा परिणाम प्राप्त करें .. `

+0

कहाँ अपने कोड है? –

+0

अपने एक्सएमएल को स्क्रीनशॉट के रूप में पोस्ट न करें, प्रश्न में वास्तविक कोड डालें। – Cerbrus

+0

इसका उत्तर यहां दिया गया था http://stackoverflow.com/questions/5715612/how-to-set-setlayoutparams-for-linear-layout-elements –

उत्तर

12

क्या आप यह कर सकते हैं: आईडी को इस बच्चे रैखिक लेआउट में सेट करें और फिर:

LinearLayout linLayout = (LinearLayout) findViewById(R.id.yourID); 
LinearLayout .LayoutParams layoutParams= new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.MATCH_PARENT);//or whatever , you can set the width and height programmatically and then setLayoutParams() 

संपादित करें:

मैं तुम्हें पहले से ही इस की कोशिश की देख, क्षमा करें। लेकिन दो रैखिक लेआउट नहीं हैं?

+0

yup दोस्त वहाँ से अधिक हैं दो रैखिक लेआउट !! आपकी प्रतिक्रिया के लिए अखरोट thnx .. :) – Manish

+0

कभी कभी हम स्पष्ट नहीं देख सकते हैं :) – vodich

6

जावा कोड में रखें यह:

int width = 300; 
int height = LayoutParams.WRAP_CONTENT; 
LinearLayout childLayout = (LinearLayout) findViewById(R.id.childLayout); 
childLayout.setLayoutParams(new LayoutParams(width, height)); 
+0

@ shreya: thnx इतनी त्वरित प्रतिक्रिया के लिए ... लेकिन मैंने यह भी कोशिश की, यह चौथी लाइन पर शून्य सूचक अपवाद देता है जहां हम लेआउट पैराम्स सेट करें :( – Manish

+0

तब मुझे लगता है कि आपको लेआउट संदर्भ सही तरीके से नहीं मिला है। बच्चे का समय अभी भी उस बिंदु पर शून्य था। कृपया अपना कोड –

+0

@ शेरिया पोस्ट करें- मैंने अभी अपनी पोस्ट अपडेट की है !! कृपया इसे चबाएं। – Manish

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

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