在android gridview中响应安排内容

时间:2016-09-07 10:58:38

标签: android gridview

我正在创建一个有主屏幕的应用程序。我试图使用gridview在每个图像下填充5个图像和textview。图像视图的高度和宽度应相等。在纵向模式下应该有2列,在横向模式下应该有3列。在这两种模式中,应该使用最大的屏幕空间,并且用户不必滚动屏幕来查看内容。我试过了,

//适配器

    public View getView(final int position, final View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    int resource = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
    int statusBarHeight = 0;
    if (resource > 0) {
        statusBarHeight = context.getResources().getDimensionPixelSize(resource);
    }

    Holder holder=new Holder();
    View rowView;

    rowView = inflater.inflate(R.layout.homescreenitems, null);
    holder.tv=(TextView) rowView.findViewById(R.id.textView1);
    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);

    holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);

    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    int ImageWidth = displayMetrics.widthPixels;
    int ImageHeight = displayMetrics.heightPixels;
    android.view.ViewGroup.LayoutParams layoutParamsImage = holder.img.getLayoutParams();
    android.view.ViewGroup.LayoutParams layoutParamsTextView = holder.tv.getLayoutParams();
    if (ImageHeight > ImageWidth) {
        int ImageWidthHeight = (ImageHeight / 3) - 20 - statusBarHeight - 40;
        layoutParamsImage.width = ImageWidthHeight;
        layoutParamsImage.height = ImageWidthHeight;
        holder.img.setLayoutParams(layoutParamsImage);

        layoutParamsTextView.width = ImageWidth / 2;
        holder.tv.setLayoutParams(layoutParamsTextView);
    }else  {
        int ImageWidthHeight = (ImageHeight / 2) - 20 - statusBarHeight - 60;
        layoutParamsImage.width = ImageWidthHeight;
        layoutParamsImage.height = ImageWidthHeight;
        holder.img.setLayoutParams(layoutParamsImage);

        layoutParamsTextView.width = ImageWidth / 3;
        holder.tv.setLayoutParams(layoutParamsTextView);
    }




    rowView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    return rowView;
}

//活性

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_homescreen);

    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.abs_layout);
    TextView textViewTitle = (TextView) findViewById(R.id.mytext);
    textViewTitle.setText("Home");
    gv=(GridView) findViewById(R.id.gridView1);
    DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
    int ImageWidth = displayMetrics.widthPixels;
    int ImageHeight = displayMetrics.heightPixels;
    if (ImageHeight > ImageWidth) {
        gv.setNumColumns(2);
    }else  {
        gv.setNumColumns(3);
    }
    gv.setAdapter(new homescreencustomadapter(this, iconNames,iconImages,this));
}

问题是,只有当用户滚动屏幕时,底部的一些内容才可见。请帮帮我。

0 个答案:

没有答案