2

我正在尝试使用自定义适配器动态添加gridview,但它只显示单行

我已经尝试了 WRAP_CONTENT,MATCH_PARENT,FILL_PARENT 的高度,但它不适合我

如果我通过像 1000 这样的静态高度,那么它表明是否有任何解决方案

//网格视图代码(mainLayout是我的父布局)

mParams=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        mParams.addRule(RelativeLayout.BELOW,playerSearchLayout.getId());

        grdPlayers=new GridView(getApplicationContext());

         grdPlayers.setLayoutParams(mParams);

         grdPlayers.setNumColumns(5);
         grdPlayers.setColumnWidth(GridView.AUTO_FIT);
         grdPlayers.setVerticalSpacing(5);
         grdPlayers.setHorizontalSpacing(5);

         mainLayout.addView(grdPlayers);  

//适配器编码

package customAdapters;

import java.util.List;



import PropertiesClasses.PlayerProperties;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PlayerAdapter extends BaseAdapter {


    private List<PlayerProperties> playerData;
    private Activity activity;
    private Context context;


    //Layouts

    RelativeLayout outerLayout;
    RelativeLayout playerDetailsLayout;
    RelativeLayout.LayoutParams params;
    ImageView userImage;
    TextView txtVotes;
    RadioButton radioVote;

    public PlayerAdapter(List<PlayerProperties> player,Activity activity,Context context)
    {
        playerData=player;
        this.activity=activity;
        this.context=context;

    }



    @Override
    public int getCount() {
        return playerData.size();

    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

        params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(10, 10, 10, 10);
        userImage=new ImageView(activity.getApplicationContext());
        userImage.setId(10000+position);
        userImage.setLayoutParams(params);
        userImage.setBackgroundResource(R.drawable.player);

        params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW,userImage.getId());
        playerDetailsLayout=new RelativeLayout(activity.getApplicationContext());
        playerDetailsLayout.setId(11000+position);
        playerDetailsLayout.setBackgroundResource(R.drawable.greybg);
        playerDetailsLayout.setLayoutParams(params);


        params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        params.addRule(RelativeLayout.BELOW,playerDetailsLayout.getId());
        radioVote=new RadioButton(activity.getApplicationContext());
        radioVote.setId(17000+position);
        radioVote.setLayoutParams(params);






        outerLayout=new RelativeLayout(activity.getApplicationContext());
        outerLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT));


        //outerLayout.addView(userImage);
        outerLayout.addView(userImage);
        outerLayout.addView(playerDetailsLayout);
        outerLayout.addView(radioVote);

        return outerLayout;

    }





} 
4

0 回答 0