ALV的颜色分为行的颜色、列的颜色和CELL的颜色。
1.行的颜色
需要在要显示的内表中添加一个字段放置颜色信息,比如COLOR(4) TYPE C .
ITAB-COLOR = 'C700'.
此外,还要根据采用的FM方式还是CLASS方式设置一下显示颜色
FM方式: LS_LAYOUT-INFO_FIELDNAME = 'COLOR'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
IS_LAYOUT = LS_LAYOUT
IT_FIELDCAT = LT_FIELDCAT
TABLES
T_OUTTAB = ITAB.
CLASS方式: SLA-INFO_FNAME = 'COLOR'.
CALL METHOD GO_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_VARIANT = GS_VARIANT
I_SAVE = 'A'
IS_LAYOUT = SLA
CHANGING
IT_OUTTAB = ITAB[]
IT_FIELDCATALOG = GT_FIELDCAT[]
EXCEPTIONS
INVALID_PARAMETER_COMBINATION = 1
PROGRAM_ERROR = 2
TOO_MANY_LINES = 3
OTHERS = 4.
2.列的颜色
设置FIELDCAT就可以了,如:TMP_FIELDCAT-EMPHASIZE = 'C500'. "列的颜色
3.CELL的颜色
需要在要显示的内表添加一个字段:cellcolors TYPE lvc_t_scol.
ls_cellcolor-color-col = '5' .
ls_cellcolor-color-int = '1' .
ls_cellcolor-color-inv = '1' .
APPEND ls_cellcolor TO list-cellcolors .
APPEND list.
例子:
REPORT Z_BARRY_ALV_CL_COLOR2 .
TYPES: BEGIN OF ilist_s ,
name1(10),
name2(10),
name3(10),
END OF ilist_s.
DATA: ilist TYPE STANDARD TABLE OF ilist_s WITH HEADER LINE .
DATA: BEGIN OF list OCCURS 0.
INCLUDE STRUCTURE ilist .
DATA: rowcolor(4) TYPE c ,
cellcolors TYPE lvc_t_scol ,
cellstyles TYPE lvc_t_styl ,
celltext TYPE alv_t_dtv .
DATA: END OF list.
DATA: ok_code LIKE sy-ucomm.
DATA: l_valid(1) TYPE c.
DATA: go_grid TYPE REF TO cl_gui_alv_grid.
DATA: gt_fieldcat TYPE lvc_t_fcat,
gs_fieldcat TYPE lvc_s_fcat,
sla TYPE lvc_s_layo.
DATA: ls_fieldcat TYPE lvc_s_fcat.
DATA ls_style TYPE lvc_s_styl .
DATA ls_cellcolor TYPE lvc_s_scol .
DATA ls_celltext TYPE alv_s_dtv .
*---------------------------------------------------------------------*
* CLASS lcl_event_receiver DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION.
PUBLIC SECTION.
METHODS handle_button_click
FOR EVENT button_click OF cl_gui_alv_grid
IMPORTING es_col_id
es_row_no.
ENDCLASS. "lcl_event_receiver DEFINITION
*---------------------------------------------------------------------*
* CLASS lcl_event_receiver IMPLEMENTATION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_button_click.
PERFORM test USING es_col_id
es_row_no.
ENDMETHOD. "handle_f4
ENDCLASS. "lcl_event_receiver IMPLEMENTATION
DATA: event_receiver TYPE REF TO lcl_event_receiver.
DATA: ii TYPE i .
START-OF-SELECTION.
CLEAR ls_fieldcat.
ls_fieldcat-fieldname = 'NAME1'.
ls_fieldcat-scrtext_l = 'NAME1'.
APPEND ls_fieldcat TO gt_fieldcat.
CLEAR ls_fieldcat.
ls_fieldcat-fieldname = 'NAME2'.
ls_fieldcat-scrtext_l = 'NAME2'.
* ls_fieldcat-emphasize = 'C610'.
APPEND ls_fieldcat TO gt_fieldcat.
CLEAR ls_fieldcat.
ls_fieldcat-fieldname = 'NAME3'.
ls_fieldcat-scrtext_l = 'NAME3'.
APPEND ls_fieldcat TO gt_fieldcat.
list-name1 = 'A'.
list-name2 = 'ARE'.
ls_cellcolor-fname = 'NAME1' .
ls_cellcolor-color-col = '5' .
ls_cellcolor-color-int = '1' .
ls_cellcolor-color-inv = '1' .
APPEND ls_cellcolor TO list-cellcolors .
APPEND list.
list-name1 = 'AA'.
list-name2 = 'BB'.
ls_cellcolor-fname = 'NAME2' .
ls_cellcolor-color-col = '3' .
ls_cellcolor-color-int = '1' .
ls_cellcolor-color-inv = '1' .
APPEND ls_cellcolor TO list-cellcolors .
APPEND list.
list-name1 = 'HOW'.
list-name2 = 'ARE'.
list-rowcolor = 'C410'.
ls_style-fieldname = 'NAME3' .
ls_style-style = cl_gui_alv_grid=>mc_style_button .
* ls_style-style = cl_gui_alv_grid=>MC_STYLE_HOTSPOT.
APPEND ls_style TO list-cellstyles .
ls_celltext-buttoncell = 'TEST'.
APPEND ls_celltext TO list-celltext.
APPEND list.
sla-stylefname = 'CELLSTYLES'.
sla-ctab_fname = 'CELLCOLORS'.
sla-info_fname = 'ROWCOLOR'.
WRITE 'a'.
CREATE OBJECT go_grid
EXPORTING
i_parent = cl_gui_container=>screen0.
CREATE OBJECT event_receiver.
SET HANDLER event_receiver->handle_button_click FOR go_grid.
CALL METHOD go_grid->set_table_for_first_display
EXPORTING
* is_variant = gs_variant
i_save = 'A'
is_layout = sla
CHANGING
it_outtab = list[]
it_fieldcatalog = gt_fieldcat[]
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.
*&---------------------------------------------------------------------*
*& Form test
*&---------------------------------------------------------------------*
FORM test USING p_es_col_id TYPE lvc_s_col
p_es_row_no TYPE lvc_s_roid.
MESSAGE i899(fi) WITH p_es_col_id-fieldname p_es_row_no-row_id .
ENDFORM. " test