简体   繁体   中英

Custom Dialog Fragment doesn't show whole of view

As you can see in my screenshot, whole of layout doesn't display in dialogFragment although it has enough space. I have no idea what is my mistake.

I expect to see a 3 * 3 table.

public class ChangeFormationDialogFragment extends DialogFragment {

    public ChangeFormationDialogFragment() {
        // Empty constructor required for DialogFragment
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);

        // request a window without the title
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return dialog;
    }

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

        GridView gridView = (GridView) view.findViewById(R.id.formation_change_grid);
        gridView.setAdapter(new FormationAdapter(getActivity(), 0));

        return view;
    }
}

It's xml file (fragment_change_formation):

<GridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/myTem_bg_boost_cards"
    android:id="@+id/formation_change_grid"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="100dp"
    android:stretchMode="columnWidth"/>

My adapter class:

public class FormationAdapter extends BaseAdapter {

    private static final String TAG = "FormationAdapter";

    private Context mContext;
    private ArrayList<String> formations;
    private int selected = 0;

    public FormationAdapter(Context context, int selected) {
        this.mContext = context;

        formations = Formation.getAllFormationsAsArrayOfStrings();
        this.selected = selected;
    }

    @Override
    public int getCount() {
        if(formations == null)
            return 0;

        return formations.size();
    }

    @Override
    public Integer getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.row_formation, null);
        }

        holder = new ViewHolder();
        holder.llFormation = (LinearLayout) convertView.findViewById(R.id.llFormation);
        holder.llFormationViews = (LinearLayout) convertView.findViewById(R.id.llFormationViews);
        holder.tvFormation = (AnyTextView) convertView.findViewById(R.id.tvFormation);
        holder.tvBuyButton = (AnyTextView) convertView.findViewById(R.id.tvBuyButton);


        holder.tvFormation.setText(formations.get(position));

        if(position > 2)
            holder.tvBuyButton.setVisibility(View.VISIBLE);

        if(position == selected)
            holder.llFormation.setBackgroundColor(mContext.getResources().getColor(R.color.myTeam_greenDark));
        else
            holder.llFormation.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));

        return convertView;
    }

    static class ViewHolder {
        LinearLayout llFormation;
        LinearLayout llFormationViews;
        AnyTextView tvFormation;
        AnyTextView tvBuyButton;
    }
}

And finally, row:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp"
        android:id="@+id/llFormation">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/llFormationViews">

        </LinearLayout>

        <com.allstarxi.widget.AnyTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvFormation"
            android:layout_gravity="center"
            style="@style/font_normal_20.white"
            custom:typeface="baltomobilebold.ttf"/>

        <com.allstarxi.widget.AnyTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvBuyButton"
            android:text="@string/myTeam_dialog_buy"
            android:layout_gravity="center"
            android:paddingTop="8dp"
            android:paddingBottom="8dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:background="@color/myTem_bg_points"
            android:textStyle="bold"
            android:visibility="gone"
            style="@style/font_normal_14.black"
            custom:typeface="baltomobilebook.ttf"/>

    </LinearLayout>

This is screenshot of what I see on my device: 在此处输入图片说明

Just try this:

Add the following code inside 'onResume()'

WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
    params.width = LayoutParams.MATCH_PARENT;
    params.height =  LayoutParams.MATCH_PARENT;
    getDialog().getWindow().setAttributes(params);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM