首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在没有任何空间的情况下精确地缩放活动以适应图像的数量

如何在没有任何空间的情况下精确地缩放活动以适应图像的数量
EN

Stack Overflow用户
提问于 2018-09-02 09:59:32
回答 1查看 42关注 0票数 0

我正面临着一个非常奇怪的问题。这里我总共有8张Gridview风格的图片,每张图片的大小正好是392×500。但当我运行app时,在最后8张图片的下方总是留有一小块地方。

以下是我的代码

代码语言:javascript
运行
复制
public class BooksPage extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    int images[] = {R.drawable.phy,R.drawable.chem,R.drawable.drawable_logy,R.drawable.drawable_geology,R.drawable.bbs,R.drawable.drawable_chartered,R.drawable.bachelor,R.drawable.bachelor};

    String stream[]= {"MSC Physics","MSC Chemistry","MSC Biology",
            "MSC Geology","BBS|BBA","CA","Bachelor","+2 Science"};
    TextView navName,navEmail;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_books_page);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("पुस्तक किनबेच");

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        View navHeaderView = navigationView.getHeaderView(0);
        navName = (TextView)navHeaderView.findViewById(R.id.nav_name);
        navEmail = (TextView)navHeaderView.findViewById(R.id.nav_mail);

        navigationView.setNavigationItemSelectedListener(this);

        RecyclerView favPlaces = (RecyclerView) findViewById(R.id.favPlaces);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        favPlaces.setLayoutManager(layoutManager);
        favPlaces.setHasFixedSize(true);
        ArrayList<BookDetails> placeList = getPlaces();

        if (!isNetworkStatusAvialable(this)) {


            Toast.makeText(this, "No Internet!", Toast.LENGTH_SHORT).show();



        }

        StaggeredAdapter staggeredAdapter = new StaggeredAdapter(this,placeList);
        favPlaces.setAdapter(staggeredAdapter);
        navName.setText(PreferenceManager.getDefaultSharedPreferences(this).getString("name","null"));
        navEmail.setText(PreferenceManager.getDefaultSharedPreferences(this).getString("email","null"));
    }
    private ArrayList<BookDetails> getPlaces() {
        ArrayList<BookDetails> details = new ArrayList<>();
        for (int index=0; index<images.length;index++){
            details.add(new BookDetails(images[index],stream[index]));
        }
        return details;
    }

   

}

我的适配器的xml代码是

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardUseCompatPadding="true"
        app:cardCornerRadius="4dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@color/colorPrimaryDark">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/placePic"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/placeName"
                android:textStyle="bold"
                android:textColor="#ffffff"
                android:fontFamily="sans-serif-condensed"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
                android:gravity="center"
                android:textSize="16sp"/>


        </LinearLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

然后我虽然可能会增加图像的大小,但无论我将图像大小设置为50×50还是1000×1000,图像的大小总是相同的,空间也是恒定的。

这是屏幕截图

同样,另一个图像也是如此

但有趣的是,当我在第八个位置使用相同的第七个位置的图像时,also.....Then空间消失器和图像完全吻合。

这是屏幕截图

这里的问题是什么?我怎样才能修复它呢?感谢您的帮助

我正在使用photoshop通过保存为Web格式来更改图像的宽度和高度。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-02 10:17:23

下面的代码可以解决这个问题。但可能需要很少的编辑。

fragment/Activity onCreate/ ocreateView中添加以下代码。在这里,您只需要项目装饰器为您的回收商缩放项目

代码语言:javascript
运行
复制
 mRecycler.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(2), true));

添加创建方法dpToPx

代码语言:javascript
运行
复制
 /**
     * Converting dp to pixel
     */
    private int dpToPx(int dp) {
        Resources r = getResources();
        return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
    }

现在,在您的活动/片段中为GridSpacingItemDecoration创建一个类

代码语言:javascript
运行
复制
 /**
     * RecyclerView item decoration - give equal margin around grid item
     */
    public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

        private int spanCount;
        private int spacing;
        private boolean includeEdge;

        GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
            this.spanCount = spanCount;
            this.spacing = spacing;
            this.includeEdge = includeEdge;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view); // item position
            int column = position % spanCount; // item column

            if (includeEdge) {
                outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
                outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

                if (position < spanCount) { // top edge
                    outRect.top = spacing;
                }
                outRect.bottom = spacing; // item bottom
            } else {
                outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
                outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
                if (position >= spanCount) {
                    outRect.top = spacing; // item top
                }
            }
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52133420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档