J'essaie d'afficher la table au centre en ScrollView
mais cela n'a pas fonctionné. Mais si je supprime ScrollView
, la table sera affichée au centre. Je ne sais pas sur quelle partie est mon erreur. Quelqu'un peut-il aider à trouver l'erreur?Comment faire TableLayout centre en ScrollView pour android
C'est le codage xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_rekod"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.asiqin.attendance.Rekod"
android:background="#E9F1F2">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button2">
<TableLayout
android:id="@+id/table"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:shrinkColumns="1"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle">
</TableLayout>
</ScrollView>
</RelativeLayout>
Voici le code java:
TableRow rowHeader = new TableRow(context);
rowHeader.setBackgroundColor(Color.parseColor("#c0c0c0"));
rowHeader.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT));
String[] headertext = {"TARIKH","ALASAN","STATUS"};
for(String c:headertext){
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT));
tv.setGravity(Gravity.CENTER);
tv.setTextSize(15);
tv.setPadding(5,5,5,5);
tv.setText(c);
// tv.setBackgroundDrawable(border1);
tv.setBackgroundResource(R.drawable.valuecellborder);
rowHeader.addView(tv);
}
Merci ANUJ GUPTA, votre première solution fonctionne bien pour moi. – NAA