gpt4 book ai didi

java - 如何在 GridView 中使项目透明

转载 作者:行者123 更新时间:2023-11-30 02:22:59 25 4
gpt4 key购买 nike

我正在处理一个 GridView,它包含 3 列和 6 行。我只想显示不包含空字符串的项目,比如设置 ""-items 透明。我怎样才能做到这一点?

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

最佳答案

解决方案一

将此添加到适配器的 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>

方案二

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 并删除所有空白字符串

关于java - 如何在 GridView 中使项目透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28222967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com