gpt4 book ai didi

android - 在android中以编程方式设置TextView TextAppearance

转载 作者:IT老高 更新时间:2023-10-28 13:22:59 25 4
gpt4 key购买 nike

我将实现一个LinearLayout,其中输入字段是根据数据库表的字段数以编程方式生成的。

不幸的是,当我尝试在 TextView 中将 textApperance 属性设置为 textApperanceLarge 时,它不起作用。下面是我的代码...

for (int i = 0; i < selectedProducts; i++) {

premLayout[i] = new LinearLayout(this);
premLayout[i].setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
premLayout[i].setOrientation(LinearLayout.HORIZONTAL);
premLayout[i].setGravity(Gravity.TOP);

premTextView[i] = new TextView(this);
premTextView[i].setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
2.0f));
premTextView[i].setTextAppearance(this, android.R.attr.textAppearanceLarge);

premTextView[i].setText(premiumChannels.get(i));
premTextView[i].setId(i + 600);

int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics());
premTextView[i].setWidth(px);

premLayout[i].addView(premTextView[i]);

最佳答案

这样使用。它会起作用的。

textView.setTextAppearance(this, android.R.style.TextAppearance_Large);

或者,从 API 23 开始,您不需要传递上下文。因此,您可以简单地调用:

textView.setTextAppearance(android.R.style.TextAppearance_Large);

如果您想同时支持 API 23 或更高版本以及更低版本,您可以使用以下方法来简化您的任务。仅当您已经针对 API 23 或更高版本时才使用以下方法。如果您的目标 API 小于 23,下面的代码将给出错误,因为新方法在其中不可用。

public void setTextAppearance(Context context, int resId) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
super.setTextAppearance(context, resId);
} else {
super.setTextAppearance(resId);
}
}

关于android - 在android中以编程方式设置TextView TextAppearance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16270814/

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