android元素离边框间距,RecyclerView Item 的分割线 距边框距离问题总结
哈, 首先? 今天Design 提了一個需求,廢話少說~~! 上圖
分割線 在中間顯示,距離左邊 邊距是66px 右邊依然是66px。
剛開始接到這個需求,感覺還闊以,挺簡單的,但是在實現中 又有問題了,
怎么設置 都不能距離左右 有間距,上網上查依然沒有解決方案。
網上有設置一個 View 然后給這個 View 設置寬高 顏色 但是 在我這里不能用,
因為我是自定義的控件 直接繼承 ViewGroup 直接用 View 會有問題 影響了滑動控件。
我的解決方案如下:
我的 item 布局 是自定義的。在不改變自定義控件前提下 加上這個間隔線。
item 布局如下非常的簡單:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/px_100"
android:clickable="true"
app:ios="true"
app:leftSwipe="true"
app:swipeEnable="true">
//自定義的TextView
android:id="@+id/tv_word"
android:layout_width="match_parent"
android:layout_height="@dimen/px_100"
android:layout_centerVertical="true"
android:layout_marginStart="@dimen/px_14"
android:drawableLeft="@drawable/icon_search_history"
android:drawablePadding="@dimen/px_14"
android:gravity="center_vertical"
android:paddingLeft="@dimen/px_66"
android:maxEms="16"
android:singleLine="true"
android:textColor="@color/color_white_transparency_10"
android:textSize="@dimen/px_21" />
android:id="@+id/bt_del"
android:layout_width="@dimen/px_140"
android:layout_height="@dimen/px_100"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="@color/search_history_delete_bg"
android:gravity="center"
android:text="刪除"
android:textColor="@color/color_white"
android:textSize="@dimen/px_22" />
然后再 Fragment 中 BindView 下一章會講 很簡單的一個標簽。
@BindView(R.id.recycler_history)
EasyRecyclerView recyclerHistory;
recyclerHistory.addItemDecoration(getRecyclerViewDivider(R.drawable.search_history_solid));
RecyclerHistory 不用說 就是RecyclerView 歷史記錄。給RecyclerView每個item添加間隔線。
/**
* 獲取分割線
*
* @param drawableId 分割線id
* @return
*/
public RecyclerView.ItemDecoration getRecyclerViewDivider(@DrawableRes int drawableId) {
DividerItemDecoration itemDecoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
itemDecoration.setDrawable(ContextCompat.getDrawable(mContext, drawableId));
return itemDecoration;
}
xml 布局 如下:
android:insetLeft="66px"
android:insetRight="66px">
然后就完美解決了這個不改變 item 布局的情況下給每個 item 添加間隔線。
希望對你們有所幫助 哈哈! 雖然沒有粉絲。
總結
以上是生活随笔為你收集整理的android元素离边框间距,RecyclerView Item 的分割线 距边框距离问题总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android布局DSL,android
- 下一篇: 基于android的视频采集系统的设计与