【问题标题】:Android Widget: Add top margin to ListView in a widgetAndroid Widget:在小部件中向 ListView 添加上边距
【发布时间】:2012-09-14 06:01:32
【问题描述】:

虽然这个问题听起来像是在欺骗其他人,但我试图在小部件中执行此操作这一事实大大限制了我在其他地方使用建议的解决方案的能力。

总结:我想在列表中最顶部的项目上方和最底部的项目下方添加间距,以使它们远离标题栏和底部栏边缘。

本质上,我正在尝试解决与Add margin above top ListView item (and below last) in Android 相同的问题,但是,我不能依赖对我的 ListView 对象的引用来执行 setHeader() 或类似的操作。

这真的不可能在 Android 小部件中实现吗?

【问题讨论】:

    标签: android listview android-widget widget android-listview


    【解决方案1】:

    我假设您正在为您的 ListView 使用适配器,因此您正在夸大额外的 listview 布局。在getView 方法中,根据列表项位置(0 和最后一个),放置底部或顶部填充。

    【讨论】:

    • 我将如何使用 RemoteViews 放置该填充?
    【解决方案2】:

    所以,这里我有适配器的覆盖功能。从这个函数你可以改变或指定边距,高度。在这里你需要获取你正在使用的布局对象,你可以从这里改变布局属性。

     @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 ;
    

    //希望有帮助

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-15
        • 1970-01-01
        • 2012-07-30
        • 2016-11-17
        • 1970-01-01
        • 1970-01-01
        • 2022-01-20
        • 1970-01-01
        相关资源
        最近更新 更多