简体   繁体   English

Android小工具:在小工具中为ListView添加上边距

[英]Android Widget: Add top margin to ListView in a widget

While this question may sound like a dupe of some others, the fact I am trying to do this in a widget significantly limits my ability to use the proposed solutions elsewhere. 虽然这个问题听起来像是对其他问题的重复,但我试图在小部件中执行此操作的事实极大地限制了我在其他地方使用建议的解决方案的能力。

Summarized: I would like to add spacing above the top-most item in the list and below the bottom-most item in order to offset them away from the title bar and bottom bar edge. 总结:我想在列表中最上面的项目上方和最下面的项目下方添加间距,以使它们偏离标题栏和底部栏边缘。

Essentially, I am trying to solve the same problem as Add margin above top ListView item (and below last) in Android , however, I cannot rely on having a reference to my ListView object in order to setHeader() or anything like that. 本质上,我正在尝试解决与Android中ListView顶部上方(和最后一个下方)添加边距相同的问题,但是,我不能依靠对ListView对象的引用来设置setHeader()或类似的东西。

Is this really impossible to do in an Android widget? 在Android小部件中这真的不可能吗?

I assume you are using an adapter for your ListView, so you are inflating additional listview layout. 我假设您正在为ListView使用适配器,所以您要增加其他Listview布局。 In a getView method, according to list item position (0 and last), place your bottom or top padding. getView方法中,根据列表项的位置(0和最后一个),放置底部或顶部填充。

So ,here i have override function of adapter.From this function you can change or specify margin,height.Here you need to take object of Layout which you are using and from this you can change layout attributes. 因此,这里我具有适配器的覆盖功能。从此功能,您可以更改或指定边距,高度。在这里,您需要获取正在使用的Layout对象,并可以从中更改布局属性。

 @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater mInflater = (LayoutInflater) context
                        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                convertView = mInflater.inflate(R.layout.drawer_list_item, null);
            }
            if (position == 0) {//for first row



                ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
                TextView txtTitle = (TextView) convertView.findViewById(R.id.title);

                RelativeLayout.LayoutParams relate = new RelativeLayout.LayoutParams(200, 200);
                relate.setMargins(150, 0, 0, 0);

                imgIcon.setLayoutParams(relate);
                RelativeLayout.LayoutParams text = new RelativeLayout.LayoutParams(300, 100);
                text.setMargins(150, 180, 0, 0);


                }
    return convertView ;

//HOPE IT HELP //希望帮助

在适配器中,在getView()期间检查其位置是0还是.getLength()并在convertView上设置LayoutParams

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM