以编程方式将按钮添加到片段 [英] Adding buttons programmatically to a fragment
问题描述
我正在尝试以编程方式将图像按钮添加到作为viewpager一部分的片段中.我尝试了不同的代码,但是即使Eclipse没有返回错误,也没有显示任何按钮.
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.
这是我的代码.
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);
}
}
}
推荐答案
您可以在基本上在onCreateView方法上实现此方法.另外,您不应将getActivity()
用于ImageButton,而应使用yourView.getContext()
.
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 :).
这篇关于以编程方式将按钮添加到片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!