2017-10-05 9 views
0

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); 
    } 

enter image description here

Répondre

1

Il y a deux façons dont vous pouvez réaliser ce que vous essayez d'atteindre

1) Utiliser une disposition relative à l'intérieur du scrollview

<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"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
> 
<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> 
</RelativeLayout> 
</ScrollView> 

2) Utiliser android: layout_gravity = "center" dans le tableau mise en page

<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:layout_gravity="center" 
android:showDividers="middle"> 

Le problème avec votre code est que vous utilisez Android: layout_centerHorizontal = "true" qui fonctionne lorsque la mise en page de parent est une mise en page relative, mais cela ne fonctionnera pas si la mise en page parente est un ScrollView.

+0

Merci ANUJ GUPTA, votre première solution fonctionne bien pour moi. – NAA