11

ビューページャーの一部であるフラグメントに画像ボタンをプログラムで追加しようとしています。別のコードを試しましたが、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);
        }
    }

}
4

2 に答える 2

0

i 個の画像を作成し、それぞれを画像ホルダーに追加しているようです。ただし、実際にイメージホルダーをメイン レイアウトに結合する部分は見当たりませんでした。基本的に、これらは作成されますが、どこにも追加されません。R.layout.fragment に 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 {...
}
于 2013-08-28T00:49:11.907 に答える
0

フラグメントにボタンを動的に追加するに関する私の回答を参照できます。

基本的に 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);
}

お役に立てれば。通常のボタンを作成していたとき、しばらく同じ問題がありました。他に何か必要な場合は、お知らせください:)。

于 2018-05-14T02:00:34.450 に答える