【问题标题】:How to add label below GridView item Android?如何在 GridView 项 Android 下方添加标签?
【发布时间】:2015-05-24 13:08:49
【问题描述】:

现在我正在开发一个启动器应用程序,我在其中创建了一个GridView,它可以从设备中获取所有已安装的应用程序。但是只有应用程序图标,没有应用程序名称。我想把应用程序名称放在它下面,所以我试着把TextView放在我的应用程序图标ImageView下面,但是我的应用程序崩溃了。任何想法我应该如何解决它?这是我的代码:非常感谢!

public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i;

            if (convertView == null) {
                i = new ImageView(Apps.this);
                i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                i.setLayoutParams(new GridView.LayoutParams(93, 93));
                i.setPadding(15, 15, 15, 15);


            } else {
                i = (ImageView) convertView;
            }

            ResolveInfo info = mApps.get(position);
            i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));

            return i;
        }

【问题讨论】:

  • 哦,我明白了!你能给我举个例子吗?

标签: android gridview textview launcher


【解决方案1】:

不要构建包含TextViewImageViewview,而是使用CompoundDrawable。您只需实现一个TextView 并将其设置为DrawableTop 并使用所需的图标。这样就可以了。

来自 XML 文件

<TextView
    android:id="@+id/my_textview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableTop="@drawable/my_icon"
    android:gravity="center"
    />

或者您可以使用以下代码以编程方式进行:

myTextView.setCompoundDrawablesWithIntrinsicBounds(null, topDrawable, null, null);

【讨论】:

    【解决方案2】:

    只需使用 TextView 作为图像容器,使用 compound drawablehttp://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds(int,%20int,%20int,%20int)

    因此,您将设置1 View to replace 2 ones,将UI 简化性能改进 作为副作用。

    一个小例子:

    public View getView(int position, View convertView, ViewGroup parent) {
            TextView t;
    
            if (convertView == null) {
                t = new TextView(Apps.this);
                t.setLayoutParams(new GridView.LayoutParams(93, 93));
                t.setPadding(15, 15, 15, 15);
    
                ResolveInfo info = mApps.get(position);
                Drawable drw = info.activityInfo.loadIcon(getPackageManager());
    
                t.setCompoundDrawablesWithIntrinsicBounds(null, drw, null, null);
                //t.setText("Some Text");
                t.setText(info.activityInfo.loadLabel(getPackageManager()).toString());
    
            } else {
                t = (TextView) convertView;
            }
    
            return t;
        }
    

    [编辑]

    一旦你返回视图(t,TextView),你可以使用getCompoundDrawables()获取drawable:http://developer.android.com/reference/android/widget/TextView.html#getCompoundDrawables()

    【讨论】:

    • 每一行代码都很好,除了info,我在那一行出错了,有什么解决办法吗?
    • 对不起,我在重构时弄乱了一些代码。固定。
    • 但我必须已经返回 i;对于图标ImageView,那么如何一次返回两个东西呢?有什么方法可以实现吗?
    • 你返回视图(t,TextView)。然后你可以使用getCompoundDrawables()developer.android.com/reference/android/widget/… 来获取drawable
    • 嘿兄弟,我使用了你上面提到的代码,但它在 Grid 中只显示应用程序图标,没有 TextView,为什么会这样?
    猜你喜欢
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 2014-11-13
    • 2014-11-03
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多