java - 在 GridLayout 的 2 行之间添加一些空间

标签 java android

This is the pattern i want to implement with

// here is my grid adapter
public class TimeSlotAdapter  extends RecyclerView.Adapter {

    private LayoutInflater mInflater;
    private String strSelectedDate;
     List<Boolean> numberList;
    private List<String> mData;
    private  TextView timetv;
    List<RelativeLayout> activeTimeSlotsArray=new ArrayList<RelativeLayout>();

       // data is passed into the constructor
    public TimeSlotAdapter(Context context, List<String> data, String strSelectedDate,TextView timetv,List<Boolean> numberList) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
        this.strSelectedDate = strSelectedDate;
        this.timetv=timetv;
        this.numberList=numberList;

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
        return new listViewHolder(view);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
       if(position==5||position==11){
           RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
           params.setMargins(0,0,0,100);
           ((listViewHolder)holder).timeslot.setLayoutParams(params);
       }

                           ((listViewHolder) holder).radioButton.setText(mData.get(position));
                    ((listViewHolder)holder).radioButton.setTextColor(R.color.inactiveslots);
                    ((listViewHolder) holder).timeslot.setBackgroundResource(R.drawable.inactive_slots);

                       }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    private class listViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        RadioButton radioButton;
        RelativeLayout timeslot;
        String strStartTime;
        int selectedPosition;

        public listViewHolder(View itemView) {
            super(itemView);
            radioButton = itemView.findViewById(R.id.radioBtton);
            timeslot = itemView.findViewById(R.id.mslot1);
            timeslot.setOnClickListener(this);
            radioButton.setOnClickListener(this);
        }
}

当我尝试提供 button 大小的空间时位置 5 和 11 已更改。这里我在 gridlayout 中给出空间但我不想改变按钮的大小,它应该是相同的大小。而且我需要两个 rows 之间的空间在 grid

最佳答案

试试这个:

   LayoutParams params = ((listViewHolder)holder).timeslot.getLayoutParams();
   params.setMargins(0,0,0,100);
   ((listViewHolder)holder).timeslot.setLayoutParams(params);

并检查R.layout.grid_item文件,如果radioButtonlayout_width属性是wrap_content

关于java - 在 GridLayout 的 2 行之间添加一些空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52233239/

相关文章:

sql-server - sqljdbc4 在更新到 Apple Java for Mac OS X 10.6 update 6 后挂起

java - JSpinner 获取最大最小值

通过 JSNI 在 GWT 中实现类似 java.lang.reflect.Proxy 的功能

android - 如何在移动应用中实现高安全性?

java - java中函数改变变量

Java初学者: Message coming up under General output in java although process was completed

java - 如何以编程方式在二维数组中插入值?

Android C Select() 性能

安卓工具栏阴影

android应用程序在运行时找不到字符串资源