gpt4 book ai didi

java - 如何使用 xml 或 java 设置 gridview 单元格的背景颜色,但它不是自定义的

转载 作者:太空宇宙 更新时间:2023-11-04 13:55:46 24 4
gpt4 key购买 nike

public class Rajesh extends Activity {

ListView ap;

ListView paypalList;
GridView gridBox;
DisplayAppListArrayAdapter displayAppListArrayAdapter;

Button turnOverButton;
String[] solutionArray = { "$0.001", "$0.010", "$0.005", "$0.003", "$0.002", "$0.008", "$0.001","$0.001", "$0.001" };
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
redeemTextUpper = new String[] {

getString(R.string.get_for_500),
getString(R.string.get_for_1000)
};


pics = new Integer[] {
R.drawable.paypal, R.drawable.paypal,

};

appList=(ListView)findViewById(R.id.appList);
paypalList=(ListView)findViewById(R.id.list_paypal);
gridBox=(GridView)findViewById(R.id.gridFit);
turnOverButton=(Button)findViewById(R.id.turnOver)
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "YOU want turnOver!", Toast.LENGTH_LONG).show();
}
});

// gridBox.setAdapter(new TextAdapter(this));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, solutionArray);
gridBox.setAdapter(adapter);
// gridBox.getChildAt(0).setBackgroundColor(Color.parseColor("#18A608"));
gridBox.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
gridBox.getChildAt(position).setBackgroundColor(Color.parseColor("#18A608"));

Toast.makeText(MainActivity.this, "You Clicked at "

+solutionArray[+ position], Toast.LENGTH_SHORT).show();

}
});
// gridBox.getChildAt(9).setBackgroundColor(Color.parseColor("#18A608"));
displayAppListArrayAdapter = new DisplayAppListArrayAdapter(
getApplicationContext(), pics, redeemTextUpper ,null);

paypalList.setAdapter(displayAppListArrayAdapter);





}



}

当我使用此代码时,背景颜色已更改,但当我单击但我想更改 oncreate() 时。

最佳答案

有两种方法可以做到这一点。

1) 通过扩展 ArrayAdapter 创建自定义 ArrayAdapter

new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, solutionArray) { 
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);

int color = 0x00FFFFFF; // Transparent
if (someCondition) {
color = 0xFF0000FF; // Opaque Blue
}

view.setBackgroundColor(color);

return view;
}
};

2) 上面的方法很简单。只需创建自定义 Textview 布局并将该布局替换为 android.R.layout.simple_list_item_1 即可,这非常简单。喜欢:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:paddingRight="?android:attr/listPreferredItemPaddingRight"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:background="#18A608" />

如果只是想改变颜色,我建议采用第二种方法。

关于java - 如何使用 xml 或 java 设置 gridview 单元格的背景颜色,但它不是自定义的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29846999/

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