我有麻烦了!我有这个RecyclerView,其中我使用GridLayoutManager来实现两列和几行。但我的问题来了:我在这个RecyclerView中最多有8个项目,我想根据屏幕大小来调整它们
到目前为止,我得到了这个:
使用这段代码:
Rect rectangle = new Rect();
Window window = ((Activity)context).getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
int contentViewTop =
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight= contentViewTop - statusBarHeight;
final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
new int[] { android.R.attr.actionBarSize });
int mActionBarSize = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
int softButtonsHeight = 0;
DisplayMetrics metrics = new DisplayMetrics();
((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
DisplayMetrics realMetrics = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
((Activity)context).getWindowManager().getDefaultDisplay().getRealMetrics(realMetrics);
if(realMetrics.heightPixels > metrics.heightPixels){
softButtonsHeight = realMetrics.heightPixels - metrics.heightPixels;
}
}
ImageView img_Logo = (ImageView)rootView.findViewById(R.id.img_logo_detalhe);
float logoHeight = 0;
//convertendo na mão tamanho do sponsor
if(img_Logo.getVisibility() != GONE) {
logoHeight = 100 * context.getResources().getDisplayMetrics().density;
}
double sizeInPx = (metrics.heightPixels - titleBarHeight - softButtonsHeight - mActionBarSize - logoHeight) / Math.round(list.size() / 2D);
itensAdapter = new OptionItensAdapter(context, list, (int)sizeInPx);
rvOptions.setAdapter(itensAdapter);
和在我的 onBindViewHolder
上的选项项适配器构造函数中:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, sizeInPx);
holder.imageButton.setLayoutParams(params);
你有什么想法可以让我做到这一点吗?提前致谢。
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.item_list, null);
int height = parent.getMeasuredHeight() / 4;
int width = parent.getMeasuredWidth();
view.setLayoutParams(new RecyclerView.LayoutParams(width, height));
return new ViewHolder(view);
}
我的建议是使用与此类似的布局,而不是回收视图,它适合任何屏幕。布局将自行完成所有需要的调整,而无需任何代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:src="@android:drawable/sym_def_app_icon" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:src="@android:drawable/sym_def_app_icon" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:src="@android:drawable/sym_def_app_icon" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:src="@android:drawable/sym_def_app_icon" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:src="@android:drawable/sym_def_app_icon" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:src="@android:drawable/sym_def_app_icon" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:src="@android:drawable/sym_def_app_icon" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:src="@android:drawable/sym_def_app_icon" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:src="@android:drawable/sym_def_app_icon" />
</LinearLayout>
</LinearLayout>
看看这个OnBindViewHolder代码,并根据您的要求进行更改:D
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
final int pos = position;
try {
//
Resources r = activity.getResources();
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics()); // i have bottom tabbar so yf you dont have any thing like this just leave 150 to 0.I think in your case height of image view an your top(Pifer)
//this change height of rcv
DisplayMetrics displaymetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.height = (height - px) / 5; //height recycleviewer (there are 5 rows so divide by 5 but i think in your case there are 4 rows so divide by 4)
viewHolder.itemView.setLayoutParams(params);
viewHolder.nameTxt.setText(totalList.get(position).getName());
viewHolder.icon.setImageResource(totalList.get(position).getIcon());
// viewHolder.background.setBackground(ContextCompat.getDrawable(context, totalList.get(position).getBackground()));
} catch (Exception e) {
e.printStackTrace();
}
}
刚刚张贴这个viewHolder看到所有项目。
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView nameTxt;
public RelativeLayout background;
public ImageView icon;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
nameTxt = (TextView) itemLayoutView.findViewById(R.id.menu_label);
background = (RelativeLayout) itemLayoutView.findViewById(R.id.menu_background);
icon = (ImageView) itemLayoutView.findViewById(R.id.menu_icon);
}
我正在使用android gif绘图库在我的android应用程序中显示gif图像。正如您所知,gif图像应该位于文件夹中,而不是位于文件夹中,因此android无法自动在不同大小之间切换。 我如何处理不同屏幕尺寸的问题?
我试图使用RecyclerView和GridLayoutManager来显示项目列表。 以下是我想要满足的标准: 每行都有一个由 我发现,如果我在项目视图上设置,那么项目宽度将通过将RecyclerView的宽度除以
问题内容: 该文档是非常难学...... 有没有谁可以教我怎么一组孩子的任何一个Views到有类似的“权重”? 现在看来,所有内容都放在了左侧, 我已经尝试了很多次,但仍然无法像每个屏幕一样将其变成屏幕宽度的一半。 编辑:我不知道当所有的小孩都可以做些什么… …即使我想以特定的尺寸设置一些图像,此类也可以帮助我做ImageView ....它无法正常运行,我错过了一些设置吗?!? 问题答案: 注意
大家好 我在react native中遇到了CSS问题,我想在没有ScrollView的情况下在屏幕中安装所有组件,但我似乎无法找出问题所在(我尝试更改边距或填充的值,但在每个设备中看起来都不一样) 正如你所看到的附加图像: 正如你所看到的,下面的图表不在屏幕上,我需要滚动查看图表(我不想要) 以下是css代码:(我删除了一些行以缩短代码(如旋转木马组件等) ); CSS:
问题内容: 我想构建一个锁屏更换应用程序。有什么方法可以创建一个侦听器/服务,该监听器/服务将在用户唤醒/解锁屏幕时启动我的应用程序? 问题答案: 请参阅mylockforandroid的源代码, 您将需要使用DeviceAdminReceiver来禁用默认的Android 屏幕锁。 当用户解锁屏幕将and 注册为时启动活动: 将此代码添加到manifast.xml中,将ScreenReceive
我在Android上实现了一个用于绘图的画布,但是绘图视图充满了屏幕。我想调整这个屏幕的大小。我该怎么办? 一旦我将MyView设置为在oncreate方法中使用setContentView显示。 mainactivity.java 这是在画布上作画的部分。但是,如前所述,我想调整一下我可以画画的范围。我不希望整个屏幕都是一个绘图视图。MyView.java