gpt4 book ai didi

java - 更改 ListView 的字体大小和文本大小

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

在我的应用程序中,我有一个包含图像按钮和 TextView 的 ListView 。我想根据用户的选择更改 ListView 的字体大小和字体大小。我的 ListView 扩展自 ArrayAdapter。 这是我的代码:

public class ListAdapter extends ArrayAdapter<String> {
customButtonListener customListner;





public interface customButtonListener {
public void onButtonClickListner(int position,String value);
}

public void setCustomButtonListner(customButtonListener listener) {
this.customListner = listener;
}

private Context context;

private ArrayList<String> data = new ArrayList<String>();

public ListAdapter(Context context, ArrayList<String> dataItem) {
super(context, R.layout.child_listview, dataItem);
this.data = dataItem;
this.context = context;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.child_listview, null);

viewHolder = new ViewHolder();

viewHolder.text = (TextView) convertView
.findViewById(R.id.childTextView);

// textView= (TextView) convertView.findViewById(R.id.childTextView);
// viewHolder.text = textView;
viewHolder.Button = (ImageButton) convertView
.findViewById(R.id.childButton);
viewHolder.text.setTextColor(Color.BLUE);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final String temp = getItem(position);
viewHolder.text.setText(temp);
viewHolder.Button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if (customListner != null) {
customListner.onButtonClickListner(position,temp);
}

}
});

return convertView;
}

public class ViewHolder {
TextView text;
ImageButton Button;
}

和我的主要 Activity :

    public class MainActivity extends Activity implements
customButtonListener {

private ListView listView;
public TextView textView;

ListAdapter adapter;
ArrayList<String> dataItems = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//G.currentActivity = this;
setContentView(R.layout.activity_main);
String[] dataArray = getResources().getStringArray(R.array.listdata);
List<String> dataTemp = Arrays.asList(dataArray);
dataItems.addAll(dataTemp);
listView = (ListView) findViewById(R.id.listView);


// tx.setTypeface(G.defaultFont);
adapter = new ListAdapter(MainActivity.this, dataItems);
adapter.setCustomButtonListner(MainActivity.this);
listView.setAdapter(adapter);


}

@Override
public void onButtonClickListner(int position, String value) {

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = dataItems.get(position);
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}

我该怎么做?有什么建议吗?

最佳答案

另请注意,如果在代码中设置了 textSize,则调用 textView.setTextSize(X) 会将数字 (X) 解释为 SP .使用 setTextSize(TypedValue.COMPLEX_UNIT_DIP, X)dp 中设置值。

关于java - 更改 ListView 的字体大小和文本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28601080/

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