以编程方式将按钮添加到一个片段中[英] Adding buttons programmatically to a fragment

本文是小编为大家收集整理的关于以编程方式将按钮添加到一个片段中的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试将图像按钮添加到ViewPager的一部分中.我尝试了不同的代码,但是即使Eclipse返回没有错误,也没有显示按钮.

我找到了一个类似的问题在这里但是答案并没有帮助我让我的按钮出现.

这是我的代码.

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

}

推荐答案

看来,您正在创建I图像数量,将它们添加到图像持有人中.但是,我没有看到您实际上加入图像持有人来进行主布局的任何部分.因此,基本上,这些是创建但未添加到任何地方的.让我们假设您的r.layout.碎片只有一个带有id framelayout1的framelayout.以下是我的建议:

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 {...
}

其他推荐答案

您可以在添加按钮中添加片段 .

基本上,在您的onCreateview方法上,实现此目标.另外,您不应将getActivity()用于imageButton,而应使用yourView.getContext().

View myView = inflater.inflate(R.layout.fragment, container, false);

 for (int i = 0; i < image_array.length; i++) {

            final ImageButton b1 = new ImageButton(myView.getContext());
            b1.setImageResource(image_array[i]);
            b1.setId(i + 1);
            b1.setOnClickListener(this);

//Change the linearlayout to relative layout for your context
//you should set another layout within your R.layout.fragment
            LinearLayout linearlayout = (LinearLayout) myView.findViewById(R.id.btnholder); 
            linearlayout.setOrientation(LinearLayout.VERTICAL);

            LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            buttonParams.setMargins(left, top, right, btm);

            linearlayout.addView(b1, buttonParams);
}

希望这会有所帮助.当我创建正常按钮时,我有一段时间的问题.如果您需要其他任何需要,请让我知道:)

本文地址:https://www.itbaoku.cn/post/2091059.html

问题描述

I'm trying to programmatically add image buttons to the fragment that is a part of a viewpager. I tried different codes, but no button shows up even though Eclipse returns no error.

I found a similar question here but the answers didn't help me make my buttons appear.

Here is my code.

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

}

推荐答案

It seems that you are creating i number of images, adding each of them to a imageholder. However, I did not see any part you actually join imageholders to main layout. So basically, these are created but not added to anywhere. Let us assume your R.layout.fragment has only a FrameLayout with id frameLayout1. Below would be my suggestion:

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 {...
}

其他推荐答案

you can refer to my answer on Adding button to fragments dynamically.

Basically on your onCreateView method, implement this. Also, you should not use getActivity() for your ImageButton but use yourView.getContext() instead.

View myView = inflater.inflate(R.layout.fragment, container, false);

 for (int i = 0; i < image_array.length; i++) {

            final ImageButton b1 = new ImageButton(myView.getContext());
            b1.setImageResource(image_array[i]);
            b1.setId(i + 1);
            b1.setOnClickListener(this);

//Change the linearlayout to relative layout for your context
//you should set another layout within your R.layout.fragment
            LinearLayout linearlayout = (LinearLayout) myView.findViewById(R.id.btnholder); 
            linearlayout.setOrientation(LinearLayout.VERTICAL);

            LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            buttonParams.setMargins(left, top, right, btm);

            linearlayout.addView(b1, buttonParams);
}

Hope this helps. I had the same problem for a while when I was creating normal button. If you need anything else, let me know :).