Dieser Beitrag zeigt, wie man in Android in einer Tabelle Anzeigeelemente
in einer Zelle zur Mitte ausrichtet.
Wenn man in Android zunächst eine Tabelle erstellt, dann
werden immer die View-Elemente innerhalb einer Zelle nur zur Linken Seite
orientiert dargestellt
Der Grund hierfür ist, dass eine eingebettetes TextView
Element in einer TableRow immer zunächst im WRAP Modus eingefügt, wird, das
heißt, es ist nur so lange, wie der Text selber ist.
Deshalb muss man wie im Design-Layout dafür sorgen, dass das
innere Element in einer Zelle automatisch so breit erzeugt wird, wie das
umfassende Zellen-Element.
Lösung:
Deshalb muss man eine Inneren View-Element immer mit dem
LayoutParameter die Darstellung zur Laufzeit zuordnen
TableRow.LayoutParams lpMatchWidth = new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);
lblMonth_Text.setLayoutParams(lpMatchWidth);
|
Volle Breite anpassen
Mit Layout_Parameter: MATCH_PARENT
Damit sich das Attribut Ausrichtung zu CENTER oder CENTER_HORIZONTAL
auswirken kann, muss das View-Element auch eine volle Breite haben.
Hierzu muss man dem TextView über LayoutParameter in der
Breite MATCH_Parent zuweisen.
TextView lblMonth_Text = new TextView(this);
lblMonth_Text.setText(sMonth);
lblMonth_Text.setGravity(Gravity.CENTER);
//<
TableRow.LayoutParams lpMatchWidth = new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);
lblMonth_Text.setLayoutParams(lpMatchWidth);
|
TableRow.LayoutParams lpMatchWidth = new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);
lblMonth_Year.setLayoutParams(lpMatchWidth);
rowHeader.addView(lblMonth_Year);
tblHeader.addView(rowHeader);
panel_Month.addView(tblHeader);
//</ Header1 >
//< Header2 >
//scroller_Vertical.setMinimumWidth(500);
TableLayout tblHeader2 = new TableLayout(this);
tblHeader2.setLayoutParams(layout_Table);
TableRow rowHeader2 = new TableRow(this);
rowHeader2.setBackgroundColor(Color.BLACK);
SimpleDateFormat fmtMonth=new SimpleDateFormat("MMM");
String sMonth=fmtMonth.format(dtMonth);
TextView lblMonth_Text = new TextView(this);
lblMonth_Text.setText(sMonth); //*cast integer to string
lblMonth_Text.setTextColor(Color.WHITE);
lblMonth_Text.setGravity(Gravity.CENTER);
lblMonth_Text.setLayoutParams(lpMatchWidth);
lblMonth_Text.setBackgroundColor(Color.BLACK);
rowHeader2.addView(lblMonth_Text);
tblHeader2.addView(rowHeader2);
panel_Month.addView(tblHeader2);
|
Referenz-Code
Kompletter Code-Block
Date dtMonth = clsDate.getDate_by_Year_Month_Day(Calendar_Year,
Calendar_Month, 1);
LinearLayout stack_Horizontal = findViewById(R.id.stack_Horizontal);
LinearLayout panel_Month=new LinearLayout(this);
panel_Month.setOrientation(LinearLayout.VERTICAL );
LinearLayout.LayoutParams lpTable=new LinearLayout.LayoutParams(_intTableWidth,ViewGroup.LayoutParams.WRAP_CONTENT);
panel_Month.setLayoutParams(lpTable);
TableLayout tblHeader = new TableLayout(this);
TableLayout.LayoutParams layout_Table=new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
tblHeader.setBackgroundColor(Color.rgb(50,50,50));
tblHeader.setLayoutParams(layout_Table);
//< Header1 >
TableRow rowHeader = new TableRow(this);
rowHeader.setBackgroundColor(Color.GRAY);
rowHeader.setBackground(ContextCompat.getDrawable(context, R.drawable.border));
TextView lblMonth_Year = new TextView(this);
lblMonth_Year.setText(String.valueOf(Calendar_Year) ); //*cast integer to string
lblMonth_Year.setTextColor(Color.BLACK);
lblMonth_Year.setBackgroundColor(Color.YELLOW);
lblMonth_Year.setGravity(Gravity.CENTER_HORIZONTAL);
TableRow.LayoutParams
lpMatchWidth = new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);
lblMonth_Year.setLayoutParams(lpMatchWidth);
rowHeader.addView(lblMonth_Year);
tblHeader.addView(rowHeader);
panel_Month.addView(tblHeader);
//</ Header1 >
//< Header2 >
//scroller_Vertical.setMinimumWidth(500);
TableLayout tblHeader2 = new TableLayout(this);
tblHeader2.setLayoutParams(layout_Table);
TableRow rowHeader2 = new TableRow(this);
rowHeader2.setBackgroundColor(Color.BLACK);
SimpleDateFormat fmtMonth=new SimpleDateFormat("MMM");
String sMonth=fmtMonth.format(dtMonth);
TextView lblMonth_Text = new TextView(this);
lblMonth_Text.setText(sMonth); //*cast integer to string
lblMonth_Text.setTextColor(Color.WHITE);
lblMonth_Text.setGravity(Gravity.CENTER);
lblMonth_Text.setLayoutParams(lpMatchWidth);
lblMonth_Text.setBackgroundColor(Color.BLACK);
rowHeader2.addView(lblMonth_Text);
tblHeader2.addView(rowHeader2);
panel_Month.addView(tblHeader2);
//</ Header2 >
|
align, align_center