Gridview及其图像并不适合所有屏幕尺寸 [英] Gridview and its images is not fit for all screensize
问题描述
就像我的标题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,因此浪费了GridView
的回收机制,方法是在实例化View
之前不检查其是否为空.
1) Right now, you're ignoring the convertView, so you're wasting GridView
s 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_gravity
和gravity
之间是有区别的–您使用的是重力,因此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屋!