gpt4 book ai didi

java - 可以更改 CustomListview 背景样式吗?

转载 作者:太空宇宙 更新时间:2023-11-04 12:28:25 25 4
gpt4 key购买 nike

我正在尝试开发一个Android应用程序。我对数组使用了自定义 ListView 。

我有 4 个这样的按钮:

i have 4 buttons like that :

但我想要:当黄色按钮单击 ListView 的样式时:

When button 1 clicked Listview's style :

当按钮2点击listview的样式时::

When Purple button clicked

我的代码如下:

自定义 TextView xml:

    android:background="@drawable/lstx_1"
android:id="@+id/ls_txt"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="40dp"/>

自定义适配器.java

public class customlist extends ArrayAdapter<String>  {
private final Activity context;
private final String[] hikayeler;
private Typeface tf;
public customlist(Activity context, String[] hikayeler) {
super(context,R.layout.lsv_txt, hikayeler);
this.context = context;
this.hikayeler = hikayeler;
this.tf = Typeface.createFromAsset(context.getAssets(), "poetsen.ttf");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.lsv_txt, null, true);
TextView ls_txt = (TextView) rowView.findViewById(R.id.ls_txt);
ls_txt.setText(hikayeler[position]);
ls_txt.setTypeface(tf);

GradientDrawable gdBaslik = new GradientDrawable();
gdBaslik.setColor(Color.parseColor("#cf96ac"));
gdBaslik.setCornerRadius(12);
gdBaslik.setStroke(2, Color.parseColor("#cf96ac"));
ls_txt.setBackground(gdBaslik);

return rowView;} }

最佳答案

示例代码如下:

private ListView mListView;
private customlist mcustomAdapter;
private boolean mStyle = false;

public void showColor(View v) {
if (mListView == null) {
mListView = (ListView) findViewById(R.id.lv_test);
mcustomAdapter = new customlist(this, new String[] { "aaaa", "bbbbb", "ccccc" });
mListView.setAdapter(mcustomAdapter);
} else {
GradientDrawable gdBaslik = new GradientDrawable();
if (mStyle) {
gdBaslik.setColor(Color.parseColor("#cf96ac"));
gdBaslik.setCornerRadius(12);
gdBaslik.setStroke(2, Color.parseColor("#cf96ac"));
} else {
gdBaslik.setColor(Color.parseColor("#849232"));
gdBaslik.setCornerRadius(12);
gdBaslik.setStroke(2, Color.parseColor("#849232"));
}
mStyle = !mStyle;
mcustomAdapter.setGradientDrawable(gdBaslik);
}
}

public class customlist extends ArrayAdapter<String> {
private final Activity context;
private final String[] hikayeler;
private GradientDrawable gdBaslik;
private Typeface tf;

public customlist(Activity context, String[] hikayeler) {
super(context, R.layout.lsv_txt, hikayeler);
this.context = context;
this.hikayeler = hikayeler;
this.tf = Typeface.createFromAsset(context.getAssets(), "poetsen.ttf");
}

public void setGradientDrawable(GradientDrawable drawable) {
this.gdBaslik = drawable;
notifyDataSetChanged();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.lsv_txt, null, true);
TextView ls_txt = (TextView) rowView.findViewById(R.id.ls_txt);
ls_txt.setText(hikayeler[position]);
ls_txt.setTypeface(tf);

ls_txt.setBackground(gdBaslik);

return rowView;
}
}

注意:“showColor(View v)”是一个按钮点击事件,希望对您有帮助。

关于java - 可以更改 CustomListview 背景样式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38143923/

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