gpt4 book ai didi

android - 为 ListView 中的特定项目设置样式

转载 作者:行者123 更新时间:2023-11-29 01:26:37 25 4
gpt4 key购买 nike

如何为 ListView 中的特定项设置样式?比方说
if(text in list view.equals("ABC")),则 ABC 应为 粗体,大小为 16sp 等。我正在创建此 ListView 来自一个字符串数组。

这是我的代码:

public class IngSearch extends AppCompatActivity implements AdapterView.OnItemClickListener, AbsListView.MultiChoiceModeListener {

ListView lv;
String list[] = {"123", "QAZ", ""edc", "ABC"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ing_search);
lv = (ListView) findViewById(R.id.list1);
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));

在这个列表中,我想将这样的样式应用于 ABC

<style name="abcStyle">
<item name = "android:textSize">24sp</item>
<item name="android:textColor">#000</item>
</style>

谢谢

最佳答案

我认为这是解决方案
首先,您需要自定义您的 listview 适配器 然后在 getView(...) 中您可以为每个特定的 textview 设置样式

      public class ListViewAdapter extends BaseAdapter {


@Override
public int getCount() {
...
}

@Override
public codeLearnChapter getItem(int arg0) {
...
}

@Override
public long getItemId(int arg0) {
...
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
...
TextView textViewTitle = (TextView) findViewById(R.id.text_view_title);
if(your condition){
textViewTitle.setTextAppearance(this, R.style.abcStyle); // change your textview style here
}else if(...){
textViewTitle.setTextAppearance(// another style );
}else{
textViewTitle.setTextAppearance(// another style );
}
}
}

希望对你有帮助

关于android - 为 ListView 中的特定项目设置样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33684866/

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