Gridview 及其图像不适合所有屏幕尺寸 [英] Gridview and its images is not fit for all screensize

查看:25
本文介绍了Gridview 及其图像不适合所有屏幕尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像我的标题 gridview 一样,图像并不适合所有屏幕.

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

在我的应用中,我有 15 张图片,它是标题,我想在所有屏幕尺寸的 3 列和 5 行格式中显示它.

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.

但我的 Gridview 不适合所有屏幕尺寸和图像,标题没有正确对齐.

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

我已经在以下 API 级别测试了我的应用并得到了以下响应.

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

Reminders.java

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);
    }
}

推荐答案

因此,问题在于您如何在此方法中创建 View,并且出现了一些错误:

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) 现在,您忽略了 convertView,因此在实例化 View 之前不检查它是否为空,从而浪费了 GridView 的回收机制.

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) 您将 GridView.LayoutParams 附加到嵌套在 LinearLayout 中的子视图.LinearLayout 应该有 GridView.LayoutParams,但 LinearLayout 的孩子应该有 LinearLayout.LayoutParams.

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) layout_gravitygravity 之间存在差异——您使用的是重力,对于 LinearLayout 而言,它不会像您认为的那样工作.(除非在这种情况下,您将 TextView 的宽度更改为 match_parent,否则可能会搞砸其他事情)

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)

我建议放弃动态创建并采用 XML 膨胀方法.

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

这篇关于Gridview 及其图像不适合所有屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆