在DialogFragment中设置自定义文本

时间:2014-05-30 08:43:10

标签: android fragment

早上好,很长一段时间我试图在我的DialogFragment中显示自定义文字,但我仍然没有得到任何内容,我在扩展FragmentActivity的活动中有多个标签并实现TabListener,这就是我调用dialog

的方式
 @Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case R.id.action_contact:
        QuickContactFragment dialog = new QuickContactFragment();
        dialog.show(getSupportFragmentManager(), "QuickContactFragment");
        return true;

    }

    return super.onOptionsItemSelected(item);
}

这是我的对话框主体:

 public class QuickContactFragment extends DialogFragment {
View view;
private PagerSlidingTabStrip tabs;
private ViewPager pager;
private ContactPagerAdapter adapter;

public static QuickContactFragment newInstance() {
    QuickContactFragment f = new QuickContactFragment();
    return f;


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
 savedInstanceState) {


     if (getDialog() != null) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

 getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);

     }

    View root = inflater.inflate(R.layout.fragment_quick_contact, container, 
 false);

    tabs = (PagerSlidingTabStrip) root.findViewById(R.id.tabs);
    pager = (ViewPager) root.findViewById(R.id.pager);
    adapter = new ContactPagerAdapter();

    pager.setAdapter(adapter);

    tabs.setViewPager(pager);

    return root;
}


@SuppressWarnings("deprecation")
@Override
public void onStart() {


    super.onStart();

    // change dialog width
    if (getDialog() != null) {

        int fullWidth = getDialog().getWindow().getAttributes().width;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            Display display = 
 getActivity().getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            fullWidth = size.x;
        } else {
            Display display = 
 getActivity().getWindowManager().getDefaultDisplay();
            fullWidth = display.getWidth();
        }

        final int padding = (int) 
  TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
                .getDisplayMetrics());

        int w = fullWidth - padding;
        int h = getDialog().getWindow().getAttributes().height;

        getDialog().getWindow().setLayout(w, h);
    }
}

public class ContactPagerAdapter extends PagerAdapter implements IconTabProvider {

    private final int[] ICONS = { R.drawable.ic_launcher_gplus, 
 R.drawable.ic_launcher_gmail,
            R.drawable.ic_launcher_gmaps,  
 R.drawable.ic_launcher_chrome };

    public ContactPagerAdapter() {
        super();
    }

 //here is implemention too

    //here shows how many tabs should it predict.. i can set N=0 and every 
 time a notification comes, set N=N+1, and then whenever delete happenes, N=N-1 ;D
    @Override
    public int getCount() {
        return ICONS.length;
    }
 ////this is implemented method by the icontabprovider.. just figure it to set image 
 on IF 
 down there,



    @Override
    public int getPageIconResId(int position) {


        return ICONS[position];
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // looks a little bit messy here
        TextView v = new TextView(getActivity());
        v.setBackgroundResource(R.color.background_window);
        if (position == 2){
            v.setText("this is page 3 :D");

 TextView tv =(TextView)getActivity().findViewById(R.id.quickcontacttext);

 tv.setText("have you heared it? just bieber is  pragnent !");
        }
        else{
        v.setText("PAGE " + (position + 1));


}



        final int padding = (int)   
 TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources()
                .getDisplayMetrics());
        v.setPadding(padding, padding, padding, padding);
        v.setGravity(Gravity.CENTER);
        container.addView(v, 0);
        return v;

    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object view) {
        container.removeView((View) view);
    }

    @Override
    public boolean isViewFromObject(View v, Object o) {
        return v == ((View) o);
    }

}
}

我需要设置文字的地方,是页面更改的地方,但是我尝试的更多,我更加沮丧,你们可以在这里寻求帮助吗?谢谢你的方式

1 个答案:

答案 0 :(得分:0)

onCreateViewDialogFragment你应该这样做:

View v = inflater.inflate(R.layout.hello_world, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("This is an instance of MyDialogFragment");

检查DialogFragment Reference