正如在我的标题grIDvIEw中,图像不适合所有屏幕.
在我的应用程序中,我有15个图像,它是标题,我想在所有屏幕尺寸的3列和5行格式显示它.
但我的GrIDvIEw不适合所有屏幕尺寸和图像,标题未正确对齐.
我已经在以下API级别测试了我的应用程序并得到了以下响应.
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,并且出现了一些问题:
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之前没有检查是否为null,从而浪费了GrIDVIEws的回收机制.
2)您将GrIDVIEw.LayoutParams附加到嵌套在linearLayout中的子视图. linearLayout应该有GrIDVIEw.LayoutParams,但linearLayout的子节点应该有linearLayout.LayoutParams.
3)layout_gravity和gravity之间有区别 – 你使用的是引力,对于linearLayout来说,它不会像你想象的那样工作. (除非在此上下文中,您将TextVIEw更改为match_parent的宽度,但那可能会使其他内容混乱)
我建议放弃动态创建并采用XML通胀方法.
总结以上是内存溢出为你收集整理的android – Gridview及其图像不适合所有屏幕大小全部内容,希望文章能够帮你解决android – Gridview及其图像不适合所有屏幕大小所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)