如何在GridView中使项目透明

时间:2015-01-29 19:27:55

标签: java android gridview

我正在使用GridView,包含3列和6行。我只想显示不包含空字符串的项目,例如设置"" - 项目是透明的。我怎么能做到这一点?

main.java代码:

final String[] MOBILE_OS = new String[] {
                "A" , "B" , "C" ,
                "D" , "E" , "F" ,
                "" , "^" , "" ,
                "<" , "" , ">" ,
                "" , "!" , "" ,
                "G" , "H" , "I"};


        gridView = (GridView) findViewById(R.id.gridView);
        gridView.setAdapter(new ButtonsAdapter(this, MOBILE_OS));

XML的代码 - 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:gravity="center"
    android:background="@drawable/buttons_background">


    <TextView
        android:id="@+id/grid_item_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:layout_marginTop="5px"
        android:textSize="27px"
        android:background="@drawable/selector"></TextView>
</LinearLayout>

Screenshot

修改

ButtonsAdapter.java

public class ButtonsAdapter extends BaseAdapter {
    private Context context;
    private final String[] mobileValues;

    public ButtonsAdapter(Context context, String[] mobileValues) {
        this.context = context;
        this.mobileValues = mobileValues;
    }

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

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from mobile.xml
            gridView = inflater.inflate(R.layout.buttons_commands, null);

            // set value into textview
            TextView textView = (TextView) gridView
                    .findViewById(R.id.grid_item_label);
            textView.setText(mobileValues[position]);

            String mobile = mobileValues[position];
        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }

    @Override
    public int getCount() {
        return mobileValues.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
}

1 个答案:

答案 0 :(得分:1)

解决方案1 ​​

在适配器的getView()方法

中添加此内容
String val = ((GridView)parent).getAdapter().getItem(position).toString();
LinearLayout ll = (LinearLayout)gridView.findViewById(R.id.item_container); // add this id in your xml
if(val.length() == 0)
    ll.setBackgroundResource(R.color.transparent);
else
    ll.setBackgroundResource(R.drawable.buttons_background);

同时在colors.xml

中添加此内容
<color name="transparent">#00ffffff</color>

解决方案2

String val = ((GridView)parent).getAdapter().getItem(position).toString();
if(val.length() == 0)
    gridView.setVisiblility(View.INVISIBLE);// or View.GONE; as per your choice
else
   gridView.setVisiblility(View.VISIBLE); 

解决方案3 取决于您以后不需要这些空白字符串

将String数组更改为ArrayList并删除所有空字符串