我想在不使用xml的情况下设置RecyclerView行的边距,因为有时我有两行,我需要动态更改边距。但是当我启动程序时,没有设置保证金。是否有人能够回答我做错了什么?
我的xml文件供了解:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:focusable="true"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:id="@+id/settingTitle"
android:textColor="@color/colorBlack"
android:layout_width="match_parent"
android:textSize="16dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/settingSubtitle"
android:layout_below="@id/settingTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
这是我的适配器上的onBind:
@Override
public void onBindViewHolder(MySettingHolder holder, int position) {
// Setting for one entry
Settings setting = settingList.get(position);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// If the settingSubtitle is empty it should be not visible and just the settingTitle
if (setting.getSettingSubtitle().equals("")) {
params.setMargins(0, 18, 0, 18);
holder.settingTitle.setLayoutParams(params);
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setVisibility(View.GONE);
} else {
// Set Margins for settingTitle
params.setMargins(0, 18, 0, 18);
holder.settingTitle.setLayoutParams(params);
// Set Margins for settingSubtitle
params.setMargins(0, 0, 0, 18);
holder.settingSubtitle.setLayoutParams(params);
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setText(setting.getSettingSubtitle());
}
}
答案 0 :(得分:0)
如果我正确理解您的问题,您希望它影响整个RecyclerView
而不是每行?以下是解决方案。
当您在进入适配器之前初始化RecyclerView
Activity/Fragment
时,如果您想影响整个RecyclerView
,onBindViewHolder
,则应设置边距。绑定您为每行提供的布局并逐行操作那些小部件,因此边距将影响每一行而不是RecyclerView
。
Activity / Fragment中的代码示例如下:
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);
答案 1 :(得分:0)
<强>解强>
我已经尝试了很多天来解决我的问题。其他答案并不是很有帮助。我的问题是我如何在没有XML
的情况下将余量设置到RecyclerView行中的TextView(因为有时我只有一个TextView而不是两个):
这是在我的情况下如何在Android中的适配器中定义新的边距:
// Create new LayoutParams object
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// Define values in dp and calculate pixels from it because margins are with pixels
int dpValueBottom = 14;
int dpValueLeft = 40;
float d = customContext.getResources().getDisplayMetrics().density;
int marginStandard = (int)(dpValueBottom * d);
int marginLeft = (int)(dpValueLeft * d);
holder.settingImage.setImageResource(setting.getSettingImageUrl());
// If the settingSubtitle is empty it should be not visible and just the settingTitle
if (setting.getSettingSubtitle().equals("")) {
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setVisibility(View.GONE);
// Get settingTitle TextView
TextView settingTitle = holder.settingTitle;
// Set margin to params
params.setMargins(marginLeft, marginStandard, 0, marginStandard);
// Set params to settingTitle
settingTitle.setLayoutParams(params);
} else {
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setText(setting.getSettingSubtitle());
}
我希望我可以帮助这个答案。