2012-06-16 12 views
10

मैं प्रोग्रामपेटिक रूप से उस खंड में छवि बटन जोड़ने की कोशिश कर रहा हूं जो व्यूअरजर का हिस्सा है। मैंने अलग-अलग कोडों की कोशिश की, लेकिन ग्रहण कोई त्रुटि नहीं देता है, भले ही कोई बटन दिखाई न दे।एक टुकड़े के लिए प्रोग्रामेटिक रूप से बटन जोड़ना

मुझे एक समान प्रश्न here मिला लेकिन उत्तर मेरे बटन प्रकट होने में मेरी सहायता नहीं करते थे।

मेरा कोड यहां है।

public class ViewPagerFragment extends Fragment { 

private ViewPagerActivity mViewPagerActivity; 
private String mId; 

public ViewPagerFragment(String id) { 
    mId = id; 
} 

@Override 
public void onAttach(Activity activity) { 
    if (activity instanceof ViewPagerActivity) { 
     mViewPagerActivity = (ViewPagerActivity)activity; 
    } 
    super.onAttach(activity); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.fragment, container, false); 

    int[] image_array = { 
      R.drawable.elebutton, 
      R.drawable.right, 
      R.drawable.middle, 
      }; 

    for (int i =0;i<image_array.length;i++){ 
      ImageButton b1 = new ImageButton(getActivity()); 
      b1.setId(100 + i); 
      b1.setImageResource(image_array[i]); 

      RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
      if (i > 0) { 
       lp.addRule(RelativeLayout.BELOW, b1.getId() - 1); 
      } 
      b1.setLayoutParams(lp); 

      ImageHolder ih = new ImageHolder(getActivity()); 
      ih.addView(b1); 


    } 

    return v; 

} 
public class ImageHolder extends FrameLayout { 

    public ImageHolder(Context context) { 
     super(context); 
     initView(context); 
    } 

    public ImageHolder(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initView(context); 
    } 

    public ImageHolder(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     initView(context); 
    } 

    private void initView(Context context){ 
     View.inflate(context, R.layout.fragment, this); 
    } 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     // TODO Auto-generated method stub 
     for(int i = 0 ; i < getChildCount() ; i++){ 
      getChildAt(i).layout(l, t, r, b); 
     } 
    } 

} 

उत्तर

0

ऐसा लगता है कि आप छवियों की संख्या बना रहे हैं, उनमें से प्रत्येक को छविधारक को जोड़ना। हालांकि, मैंने मुख्य भाग में मुख्य रूप से छविधारकों में शामिल होने वाले किसी भी हिस्से को नहीं देखा। तो मूल रूप से, ये बनाए जाते हैं लेकिन कहीं भी नहीं जोड़े जाते हैं। आइए मान लें कि आपके R.layout.fragment में id frameLayout1 के साथ केवल एक फ्रेमलाउट है। नीचे मेरा सुझाव होगा:

public class ViewPagerFragment extends Fragment { 

private ViewPagerActivity mViewPagerActivity; 
private String mId; 

public ViewPagerFragment(String id) { 
    mId = id; 
} 

@Override 
public void onAttach(Activity activity) { 
    if (activity instanceof ViewPagerActivity) { 
     mViewPagerActivity = (ViewPagerActivity)activity; 
    } 
    super.onAttach(activity); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment, container, false); 
} 
// IMPORTANT PART! Here we will add images after fragment is inflated and instantiated 
@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    // Get the root layout of fragment, we called it frameLayout1 
    FrameLayout fl = (FrameLayout)(this.getActivity.findViewById(R.id.frameLayout1)); 
    int[] image_array = { 
      R.drawable.elebutton, 
      R.drawable.right, 
      R.drawable.middle, 
      }; 

    for (int i =0;i<image_array.length;i++){ 
      ImageButton b1 = new ImageButton(getActivity()); 
      b1.setId(100 + i); 
      b1.setImageResource(image_array[i]); 

      RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
      if (i > 0) { 
       lp.addRule(RelativeLayout.BELOW, b1.getId() - 1); 
      } 
      b1.setLayoutParams(lp); 

      ImageHolder ih = new ImageHolder(getActivity()); 
      ih.addView(b1); 
      fl.addView(ih); 
    } 
} 
// End of important part 
public class ImageHolder extends FrameLayout {... 
} 
संबंधित मुद्दे