Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在不带xml的RecyclerView适配器中设置页边距_Java_Android_Xml - Fatal编程技术网

Java 在不带xml的RecyclerView适配器中设置页边距

Java 在不带xml的RecyclerView适配器中设置页边距,java,android,xml,Java,Android,Xml,我想在不使用xml的情况下设置RecyclerView行的边距,因为有时我有两行,需要动态更改边距。但是,当我启动该计划时,没有设定利润。有没有人能回答我做错了什么 了解我的xml文件: 如果我正确理解您的问题,您希望它影响整个回收视图,而不是每一行?下面是一个解决方案 当您在进入适配器之前在活动/片段中初始化RecyclerView时,如果您想影响整个RecyclerView,onBindViewHolder绑定您为每一行提供的布局并在那里操作这些小部件,您应该在那里设置边距,一行一行,这样

我想在不使用xml的情况下设置RecyclerView行的边距,因为有时我有两行,需要动态更改边距。但是,当我启动该计划时,没有设定利润。有没有人能回答我做错了什么

了解我的xml文件:


如果我正确理解您的问题,您希望它影响整个
回收视图,而不是每一行?下面是一个解决方案

当您在进入适配器之前在
活动/片段中初始化
RecyclerView
时,如果您想影响整个
RecyclerView
onBindViewHolder
绑定您为每一行提供的布局并在那里操作这些小部件,您应该在那里设置边距,一行一行,这样边距将影响每一行,而不是您的
RecyclerView

活动/片段中的代码示例如下:

 recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
 recyclerView.setHasFixedSize(true);
 RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
 params.setMargins(0, 18, 0, 18);
 recyclerView.setLayoutParams(params);

解决方案

我已经试了很多天来解决我的问题。其他的答案并不是很有帮助。我的问题是,如何在没有
XML
的情况下将边距设置为RecyclerView行中的TextView(因为有时我只有一个TextView而不是两个):

以下是在我的情况下如何定义Android适配器中的新边距:

//创建新的LayoutParams对象
RelativeLayout.LayoutParams params=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_父项,RelativeLayout.LayoutParams.WRAP_内容);
//定义dp中的值并从中计算像素,因为边距与像素相同
int dpValueBottom=14;
int dpValueLeft=40;
float d=customContext.getResources().getDisplayMetrics().density;
int margininstandard=(int)(dpValueBottom*d);
int marginLeft=(int)(dpValueLeft*d);
holder.settingImage.setImageResource(setting.getSettingImageUrl());
//如果settingSubtitle为空,则应不可见,仅显示settingTitle
if(setting.getSettingSubtitle()等于(“”){
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setVisibility(视图已消失);
//获取设置标题文本视图
TextView settingTitle=holder.settingTitle;
//将边距设置为params
参数设置边距(marginLeft、marginStandard、0、marginStandard);
//将参数设置为settingTitle
settingTitle.setLayoutParams(参数);
}否则{
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setText(setting.getSettingSubtitle());

}
您应该将参数设置为recyclerview@santoshkumar你认为我在这里干什么?recyclerview.setParams(params)@santoshkumar我不知道你的意思我该怎么做不我想影响每一排。有时我有一行有两个标题,有时只有一个标题。因此,每一行都需要自己的边距,这取决于该行中的文本视图。看看我的if语句。你明白我的意思了,没问题,伙计。当我回到笔记本电脑上时,我会编辑我的答案。嘿,伙计,你的答案是什么?:)我还在等我的道歉,伙计。我明天会整理它,今天工作很忙。你能发布完整的适配器代码吗?你从哪里获得活动中设置的数据,请伙计。