简体   繁体   中英

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.

Is this really impossible to do in an Android widget?

I assume you are using an adapter for your ListView, so you are inflating additional listview layout. In a getView method, according to list item position (0 and last), place your bottom or top padding.

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.

 @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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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