我正在使用回收器视图和卡片视图来显示应用程序中的图标。
我无法弄清楚如何调整应用程序图标的大小并根据屏幕尺寸自动增加/减少跨度计数。这是我正在做的显示它的操作,并且花费计数固定为 3。
RecyclerView mrv = (RecyclerView) findViewById(R.id.recyclerview_id);
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this, lsStore);
mrv.setLayoutManager(new GridLayoutManager(this, 3));
mrv.setAdapter(myAdapter);
XML 如下
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardview_id"
android:clickable="true"
android:foreground="?selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:layout_marginVertical="20dp"
cardview:cardCornerRadius="6dp">
<android.support.v7.widget.LinearLayoutCompat
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/store_image_id"
android:layout_width="wrap_content"
android:layout_height="110dp"
android:scaleType="centerCrop"
android:background="#2D2D2D"/>
<TextView
android:id="@+id/store_name_id"
android:textColor="#2d2d2d"
android:textSize="13sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/StoreTitle"/>
</android.support.v7.widget.LinearLayoutCompat>
该应用程序在 5 英寸 5 英寸到 6 英寸左右的屏幕上表现良好,但在更小/更大的屏幕上,图标看起来很混乱。有人可以指导我如何实现这一目标吗?
您可以计算列数。定义一个静态函数来计算列数:
public class Utility {
public static int calculateNoOfColumns(Context context) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
int noOfColumns = (int) (dpWidth / 180);
// Where 180 is the width of your grid item. You can change it as per your convention.
return noOfColumns;
}
}
然后,当使用GridLayoutManager
在活动或片段中,您可以这样做:
int mNoOfColumns = Utility.calculateNoOfColumns(getApplicationContext());
// Some code...
mGridLayoutManager = new GridLayoutManager(this, mNoOfColumns);
mRecyclerView.setLayoutManager(mGridLayoutManager);
// Some code...
我希望这可以帮助你。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)