Android View 上下左右四種間距的設定方法

來源:互聯網
上載者:User

標籤:上下左右   put   hashmap   cimage   logs   rect   設定   cycle   auto   

RecyclerView控制項大家肯定不陌生,已經應用有一段時間了,最近在項目中寫一個GridLayout樣式的RecyclerView時需要設定,item之間左右的間距,下面是我總結的一個設定間距的方法分享給大家。下面是沒間距的情況
 

想要設定item之間的間距需要自己建立一個繼承自RecyclerView.ItemDecoration的類

public class RecyclerViewSpacesItemDecoration extends RecyclerView.ItemDecoration {   private HashMap<String, Integer> mSpaceValueMap;   public static final String TOP_DECORATION = "top_decoration";   public static final String BOTTOM_DECORATION = "bottom_decoration";   public static final String LEFT_DECORATION = "left_decoration";   public static final String RIGHT_DECORATION = "right_decoration";?   public RecyclerViewSpacesItemDecoration(HashMap<String, Integer> mSpaceValueMap) {       this.mSpaceValueMap = mSpaceValueMap;   }?   @Override   public void getItemOffsets(Rect outRect, View view,                               RecyclerView parent, RecyclerView.State state) {       if (mSpaceValueMap.get(TOP_DECORATION) != null)           outRect.top = mSpaceValueMap.get(TOP_DECORATION);       if (mSpaceValueMap.get(LEFT_DECORATION) != null)           outRect.left = mSpaceValueMap.get(LEFT_DECORATION);       if (mSpaceValueMap.get(RIGHT_DECORATION) != null)           outRect.right = mSpaceValueMap.get(RIGHT_DECORATION);       if (mSpaceValueMap.get(BOTTOM_DECORATION) != null)           outRect.bottom = mSpaceValueMap.get(BOTTOM_DECORATION);   }}
下面是 設定RecyclerView間距的關鍵方法
HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.TOP_DECORATION,50);//top間距stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.BOTTOM_DECORATION,100);//底部間距stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.LEFT_DECORATION,50);//左間距stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.RIGHT_DECORATION,100);//右間距mRecyclerView.addItemDecoration(newRecyclerViewSpacesItemDecoration(stringIntegerHashMap));

可以根據自己的實際情況去設定想要的間距,也可以去單獨設定
下面是設定間距後的

Android View 上下左右四種間距的設定方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.