我一直在尝试动态创建我的表,方法是将TextView添加到线性布局作为列表标题。
我试图将TextView的文字颜色设置为白色(代码在下方),但它仍然是相同的黑色文字。
我的TextView包装器:
public static class HeaderTextView implements HeaderBox {
Context mContext;
TextView textView;
protected String text;
public HeaderTextView(Context context, String text, int widthCoefficient) {
this.text = text;
this.mContext = context;
textView = new TextView(context);
textView.setTextColor(ContextCompat.getColor(context, R.color.white));
textView.setTextAppearance(context, android.R.style.TextAppearance_DeviceDefault_Medium);
textView.setLayoutParams(
new ViewGroup.LayoutParams(
Sizer.dpAsPixels(
context,
widthCoefficient * Grid.COLUMN_WIDTH
),
ViewGroup.LayoutParams.WRAP_CONTENT
)
);
textView.setText(text);
}
public View toView() {
return textView;
}
由另一个函数使用:
public static View createGridHeader(Context context, HeaderBox... columns) {
LinearLayout tableHeader = new LinearLayout(context);
tableHeader.setOrientation(LinearLayout.HORIZONTAL);
tableHeader.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
tableHeader.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
tableHeader.setGravity(Gravity.BOTTOM);
for(int i = 0; i < columns.length; i++) {
tableHeader.addView(columns[i].toView());
}
return tableHeader;
}
最后,我将这些视图添加到LinearLayout:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
ListView raporList = (ListView) view.findViewById(R.id.grid_rapor);
LinearLayout raporHeader = (LinearLayout) view.findViewById(R.id.main_linear);
raporHeader.addView(
Grid.createGridHeader(
getContext(),
new Grid.HeaderEditBox(getContext(), "TEEEEST", 4),
new Grid.HeaderEditBox(getContext(), "Teeest 2", 14),
new Grid.HeaderTextView(getContext(), "DENEME :)", 4)
),0
);
view.invalidate();
super.onViewCreated(view, savedInstanceState);
}
setTextColor或setTextAppearance似乎都不起作用。
提前谢谢。