Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

android - Gridview and its images is not fit for all screensize

As in my title gridview and it is images are not get fit for all screens .

In my app I have 15 images and it is titles ,I wanna show it on a 3 column and 5 row format in all screen sizes.

But my Gridview is not fit for all screen sizes and images,titles are not get properly aligned .

I have tested my app in following API levels and got the below response .

Reminders.java

public class Reminders extends Fragment {

    private OnFragmentInteractionListener mListener;
    private  View rootView;

    public Reminders() {

    }

    public static Reminders newInstance(String param1, String param2) {
        Reminders fragment = new Reminders();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_reminders, container, false);
        GridView gridView = (GridView) rootView.findViewById(R.id.photogridview);
        gridView.setAdapter(new ImageAdapter(rootView.getContext())); // uses the view to get the context instead of getActivity().
        return  rootView;
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So, the issue is with how you're creating the View in this method, and there's a couple of things going wrong:

public View getView(int position, View convertView, ViewGroup parent) {

    LinearLayout linearlayout=new LinearLayout(mContext);
    ImageView imageView = new ImageView(mContext);
    TextView textView =new TextView(mContext);
    textView.setGravity(Gravity.CENTER);
    linearlayout.setOrientation(LinearLayout.VERTICAL);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    imageView.setLayoutParams(new GridView.LayoutParams(230, 230));
    textView.setText(mThumbTxt[position]);

    linearlayout.addView(imageView);
    linearlayout.addView(textView);
    return linearlayout;
}

1) Right now, you're ignoring the convertView, so you're wasting GridViews recycling mechanism by not checking if that is null before instantiating the View.

2) You're attaching GridView.LayoutParams to a child View nested within a LinearLayout. The LinearLayout should have GridView.LayoutParams, but the LinearLayout's children should have LinearLayout.LayoutParams.

3) There's a difference between layout_gravity, and gravity -- you're using gravity, which for LinearLayout won't work as you think it should. (Unless in this context you change your TextView to be match_parent for width, but then that might mess other things up)

I'd recommend ditching the dynamic creation and taking an XML inflation approach.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...