listView item 增加间距 以及item根部局 margin 失效原因

最近根据设计图写listView 的时候,listView个item 之间存在间距,左右上下都有,一开始的想法是在item 的根布局 设置margin 属性,但是在listView 中margin 无法生效,所以在此研究下失效的原因。而解决办法就是在加一层布局,作为根布局 设置padding ,或者 新增根布局的下一次布局设置margin.

下面直接看效果图:
这里写图片描述


1.解决办法,我这里使用的是再嵌套一层布局加padding的方案

这里写图片描述

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:paddingTop="5dp"
              android:paddingBottom="5dp"
              android:paddingLeft="8dp"
              android:paddingRight="8dp"
              android:background="@color/transparent">

    <!-- 最上层控制item 的间隔的 -->
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:background="@drawable/home_listitem_style">

当然你也可以选择 在再嵌套一层布局的下面的一层布局,使用margin 一样的效果,如果没有左右的间距只有上下的间距,还有一种方案就是 设置dividerHeight 的高度,颜色为透明就可。

2.着重研究的是margin失效的原因

这是我看到的一段解释,
The fact is that, the margin of LinearLayout (child) asks its parent layout (container) to give child layout a margin of x value.

So if the parent layouts’ LayoutParams support the margins then that margin is honored and applied.

ListView uses AbsListView.LayoutParams by default, which doesn’t include any margin support, just the height and width, thats why, it simply ignores the params value for margins.

  • 2.1 ListView extends AbsListView 查看 AbsListView 如何加入布局参数的

这里写图片描述

直接使用的是ViewGroup的 layoutParams, 没有包含 margin 的信息,在viewGroup 源码中发现

这里写图片描述

有个叫 MarginLayoutParams 的类,继续查看他的实现类 ,其中并没有发现AbsListView,

这里写图片描述

而在 ViewGroup layoutParams 中的实现类中却发现了 AbsListView
这里写图片描述

由此说明 listView 根布局中 设置margin 直接被忽略了,因为没有实现 marginLayoutParams ,虽然子类LinearLayout 里面设置margin 是有效的,但是其父类 AbsListView layoutparams 并没有实现 marginLayoutParams,没有提供相关的margin 的值,所以子类中也获取不到对应的值,尽管已经设置了。

参考的资料:
http://stackoverflow.com/questions/16278159/why-linearlayouts-margin-is-being-ignored-if-used-as-listview-row-view

3.为什么margin 失效,padding 却有效呢?

我个人的理解是:padding 是内边距,不需要父类,自己就是父类,就可以提供子类具体的距离和值等,而margin 是外边距,需要父类提供具体的距离,当父类不支持就无法获取到对应的值。

LinearLayout extends ViewGroup extends View

view 本身就可以提供padding 的属性,而margin 却需要父类的支持。

这里写图片描述

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
WinForms中的ListViewItem按钮是一种在ListView中显示按钮的控件。ListView控件是WinForms中用于展示和编辑大量项目的强大控件。它允许我们以表格的形式显示数据,并且可以自定义每个项的外观和行为。 在ListView中显示按钮,可以为每个ListViewItem添加一个按钮来执行自定义操作。为ListViewItem添加按钮的一种常见方法是使用SubItems属性。每个ListViewItem的SubItems属性可以添加多个子项,而每个子项可以包含一个按钮。通过为每个按钮设置Click事件处理程序,我们可以为按钮定义自定义操作。 例如,我们可以创建一个包含按钮的ListViewItem列表,当用户单击按钮时,会弹出一个消息框显示该按钮所在的ListViewItem的文本。以下是一个示例代码: ```csharp ListView listView = new ListView(); // 创建按钮列 listView.Columns.Add("操作", 100); // 创建ListViewItem ListViewItem item1 = new ListViewItem("项1"); item1.SubItems.Add(new ListViewItem.ListViewSubItem(item1, "按钮1")); item1.SubItems[1].Tag = item1; // 为了在按钮的Click事件处理程序中获取ListViewItem // 添加按钮到ListViewItem Button button1 = new Button(); button1.Text = "点击我"; button1.Click += (sender, e) => { ListViewItem clickedItem = (ListViewItem)((Button)sender).Tag; MessageBox.Show(clickedItem.Text); }; listView.Controls.Add(button1); // 将按钮添加到ListView控件中 listView.Items.Add(item1); // 将ListViewItem添加到ListView控件的项目列表中 ``` 这样,当用户点击按钮时,将会弹出一个消息框,显示按钮所在的ListViewItem的文本。这就是如何使用WinForms的ListViewListViewItem按钮来实现自定义操作的简单示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值