2017-09-15 6 views
0

j'ai créer une mise en page de table comme suit:je ne peux pas régler la largeur des colonnes dans une mise en page de tableau dynamique Android

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/LinearLayout01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 
<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="0,1" 
    android:id="@+id/headertable" > 

</TableLayout> 
<ScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:layout_constraintTop_toBottomOf="@+id/headertable" 
    android:scrollbars="none"> 
    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:stretchColumns="0,1" 
     android:id="@+id/maintable" > 

    </TableLayout> 
</ScrollView> 

.java:

public class tab_stats extends Fragment { 
TextView Position, Player, 
MatchPlayed,Wins,Deuce,Lost,GoalFavor,GoalAgainst,GoalDiference,Points; 
TableRow tr,mtr; 
TableLayout mt,ht; 
String os[]  = {"Androidddddd","Mango","iOS","Symbian","Bada", 
     "Android","Mango","iOS","Symbian","Bada", 

"Android","Mango","iOS","Symbian","Bada","Bada","Bada","Bada","Bada","Bada"}; 

@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.tab_stats, container, false); 
    mt = v.findViewById(R.id.maintable); 
    ht = v.findViewById(R.id.headertable); 
    return v; 
} 

@Override 
public void onStart() { 
    super.onStart(); 
    addHeaders(); 
    addData(); 
} 

@Override 
public void onResume() { 

    super.onResume(); 
} 

@Override 
public void onDestroyView() { 
    super.onDestroyView(); 
} 

/** getActivity()function add the headers to the table **/ 
public void addHeaders(){ 
    /** Create a TableRow dynamically **/ 
    tr = new TableRow(getActivity()); 
    tr.setLayoutParams(new LayoutParams(
      LayoutParams.MATCH_PARENT, 
      LayoutParams.WRAP_CONTENT)); 
    tr.setBackgroundColor(Color.DKGRAY); 
    tr.setPadding(0, 0, 0, 2); //Border between rows 

    /** Creating a TextView to add to the row **/ 
    TextView Position = new TextView(getActivity()); 
    Position.setText("#"); 
    Position.setTextColor(Color.GRAY); 
    Position.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
    Position.setPadding(5, 5, 5, 0); 
    Position.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
    Position.setWidth(20); 
    tr.addView(Position); // Adding textView to tablerow. 

    /** Creating another textview **/ 
    TextView Player = new TextView(getActivity()); 
    Player.setText("Player"); 
    Player.setTextColor(Color.GRAY); 
    Player.setPadding(5, 5, 5, 0); 
    Player.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
    Player.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
    Player.setWidth(160); 
    tr.addView(Player); // Adding textView to tablerow. 

    /** Creating another textview **/ 
    TextView MatchPlayed = new TextView(getActivity()); 
    MatchPlayed.setText("MP"); 
    MatchPlayed.setTextColor(Color.GRAY); 
    MatchPlayed.setPadding(5, 5, 5, 0); 
    MatchPlayed.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
    MatchPlayed.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
    MatchPlayed.setWidth(60); 
    tr.addView(MatchPlayed); // Adding textView to tablerow. 

    /** Creating another textview **/ 
    TextView Wins = new TextView(getActivity()); 
    Wins.setText("W"); 
    Wins.setTextColor(Color.GRAY); 
    Wins.setPadding(5, 5, 5, 0); 
    Wins.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
    Wins.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
    Wins.setWidth(60); 
    tr.addView(Wins); // Adding textView to tablerow. 

    /** Creating another textview **/ 
    TextView Deuce = new TextView(getActivity()); 
    Deuce.setText("D"); 
    Deuce.setTextColor(Color.GRAY); 
    Deuce.setPadding(5, 5, 5, 0); 
    Deuce.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
    Deuce.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
    Deuce.setWidth(60); 
    tr.addView(Deuce); // Adding textView to tablerow. 

    /** Creating another textview **/ 
    TextView Lost = new TextView(getActivity()); 
    Lost.setText("L"); 
    Lost.setTextColor(Color.GRAY); 
    Lost.setPadding(5, 5, 5, 0); 
    Lost.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
    Lost.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
    Lost.setWidth(60); 
    tr.addView(Lost); // Adding textView to tablerow. 

    /** Creating another textview **/ 
    TextView GoalFavor = new TextView(getActivity()); 
    GoalFavor.setText("GF"); 
    GoalFavor.setTextColor(Color.GRAY); 
    GoalFavor.setPadding(5, 5, 5, 0); 
    GoalFavor.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
    GoalFavor.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
    GoalFavor.setWidth(60); 
    tr.addView(GoalFavor); // Adding textView to tablerow. 

    /** Creating another textview **/ 
    TextView GoalAgainst = new TextView(getActivity()); 
    GoalAgainst.setText("GA"); 
    GoalAgainst.setTextColor(Color.GRAY); 
    GoalAgainst.setPadding(5, 5, 5, 0); 
    GoalAgainst.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
    GoalAgainst.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
    GoalAgainst.setWidth(60); 
    tr.addView(GoalAgainst); // Adding textView to tablerow. 

    /** Creating another textview **/ 
    TextView GoalDiference = new TextView(getActivity()); 
    GoalDiference.setText("GD"); 
    GoalDiference.setTextColor(Color.GRAY); 
    GoalDiference.setPadding(5, 5, 5, 0); 
    GoalDiference.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
    GoalDiference.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
    GoalDiference.setWidth(60); 
    tr.addView(GoalDiference); // Adding textView to tablerow. 

    /** Creating another textview **/ 
    TextView Points = new TextView(getActivity()); 
    Points.setText("Pts"); 
    Points.setTextColor(Color.GRAY); 
    Points.setPadding(5, 5, 5, 0); 
    Points.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
    Points.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
    Points.setWidth(60); 
    tr.addView(Points); // Adding textView to tablerow. 

    // Add the TableRow to the TableLayout 
    ht.addView(tr, new TableLayout.LayoutParams(
      LayoutParams.MATCH_PARENT, 
      LayoutParams.WRAP_CONTENT)); 
} 

/** getActivity()function add the data to the table **/ 
public void addData(){ 
    getdata(); 

    for (int i = 0; i < os.length; i++) 
    { 
     /** Create a TableRow dynamically **/ 
     mtr = new TableRow(getActivity()); 
     mtr.setLayoutParams(new LayoutParams(
       LayoutParams.MATCH_PARENT, 
       LayoutParams.WRAP_CONTENT)); 



     /** Creating a TextView to add to the row **/ 
     Position = new TextView(getActivity()); 
     Position.setText(String.format(Locale.US, "%d", i+1)); 
     Position.setTextColor(Color.RED); 
     Position.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
     Position.setPadding(5, 5, 5, 5); 
     Position.setWidth(20); 
     Position.setBackgroundResource(R.drawable.cell_shape); 
     Position.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
     mtr.addView(Position); // Adding textView to tablerow. 


     /** Creating another textview **/ 
     Player = new TextView(getActivity()); 
     Player.setText(os[i]); 
     Player.setTextColor(Color.GREEN); 
     Player.setPadding(5, 5, 5, 5); 
     Player.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
     Player.setBackgroundResource(R.drawable.cell_shape); 
     Player.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
     Player.setWidth(160); 
     mtr.addView(Player); // Adding textView to tablerow. 

     /** Creating another textview **/ 
     MatchPlayed = new TextView(getActivity()); 
     MatchPlayed.setText("199"); 
     MatchPlayed.setTextColor(Color.GREEN); 
     MatchPlayed.setPadding(5, 5, 5, 5); 
     MatchPlayed.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
     MatchPlayed.setBackgroundResource(R.drawable.cell_shape); 
     MatchPlayed.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
     MatchPlayed.setWidth(60); 
     mtr.addView(MatchPlayed); // Adding textView to tablerow. 

     /** Creating another textview **/ 
     Wins = new TextView(getActivity()); 
     Wins.setText("999"); 
     Wins.setTextColor(Color.GREEN); 
     Wins.setPadding(5, 5, 5, 5); 
     Wins.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
     Wins.setBackgroundResource(R.drawable.cell_shape); 
     Wins.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
     Wins.setWidth(60); 
     mtr.addView(Wins); // Adding textView to tablerow. 


     /** Creating another textview **/ 
     Deuce = new TextView(getActivity()); 
     Deuce.setText("100"); 
     Deuce.setTextColor(Color.GREEN); 
     Deuce.setPadding(5, 5, 5, 5); 
     Deuce.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
     Deuce.setBackgroundResource(R.drawable.cell_shape); 
     Deuce.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
     Deuce.setWidth(60); 
     mtr.addView(Deuce); // Adding textView to tablerow. 

     /** Creating another textview **/ 
     Lost = new TextView(getActivity()); 
     Lost.setText("555"); 
     Lost.setTextColor(Color.GREEN); 
     Lost.setPadding(5, 5, 5, 5); 
     Lost.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
     Lost.setBackgroundResource(R.drawable.cell_shape); 
     Lost.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
     Lost.setWidth(60); 
     mtr.addView(Lost); // Adding textView to tablerow. 

     /** Creating another textview **/ 
     GoalFavor = new TextView(getActivity()); 
     GoalFavor.setText("444"); 
     GoalFavor.setTextColor(Color.GREEN); 
     GoalFavor.setPadding(5, 5, 5, 5); 
     GoalFavor.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
     GoalFavor.setBackgroundResource(R.drawable.cell_shape); 
     GoalFavor.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
     GoalFavor.setWidth(60); 
     mtr.addView(GoalFavor); // Adding textView to tablerow. 

     /** Creating another textview **/ 
     GoalAgainst = new TextView(getActivity()); 
     GoalAgainst.setText("1"); 
     GoalAgainst.setTextColor(Color.GREEN); 
     GoalAgainst.setPadding(5, 5, 5, 5); 
     GoalAgainst.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
     GoalAgainst.setBackgroundResource(R.drawable.cell_shape); 
     GoalAgainst.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
     GoalAgainst.setWidth(60); 
     mtr.addView(GoalAgainst); // Adding textView to tablerow. 

     /** Creating another textview **/ 
     GoalDiference = new TextView(getActivity()); 
     GoalDiference.setText("99"); 
     GoalDiference.setTextColor(Color.GREEN); 
     GoalDiference.setPadding(5, 5, 5, 5); 
     GoalDiference.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
     GoalDiference.setBackgroundResource(R.drawable.cell_shape); 
     GoalDiference.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
     GoalDiference.setWidth(60); 
     mtr.addView(GoalDiference); // Adding textView to tablerow. 

     /** Creating another textview **/ 
     Points = new TextView(getActivity()); 
     Points.setText("742"); 
     Points.setTextColor(Color.GREEN); 
     Points.setPadding(5, 5, 5, 5); 
     Points.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
     Points.setBackgroundResource(R.drawable.cell_shape); 
     Points.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
     Points.setWidth(60); 
     mtr.addView(Points); // Adding textView to tablerow. 

     // Add the TableRow to the TableLayout 
     mt.addView(mtr, new TableLayout.LayoutParams(
       LayoutParams.MATCH_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
    } 
} 

public void getdata(){ 

} 
} 

je suis en train de créer les lignes dynamiquement cela dépend donc du nombre d'amis que la personne a. Le problème est que lors de l'exécution du programme dans différents téléphones portables, la table peut sembler très bien car elle peut sembler déformée. Comment est-ce que je peux faire la première colonne être fixée et l'autre pour obtenir l'arrangement selon la taille de l'information à l'intérieur?

c'est à quoi il ressemble:

enter image description here

je veux la première colonne à fixer à 3 chiffres et les autres colonnes pour ajuster juste

Merci!

Répondre

0

Faites-vous référence à la colonne #? Vous créez avec ces paramètres:

Position.setWidth(20); 

.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 

Vous pouvez supprimer ceux-ci et les ajouter:

  android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      //Puts the text to the right of the textview 
      android:gravity="right" 

      //Puts the text view to the far left of screen 
      android:layout_gravity="left" 

      //Ensures all # column text views are the same width. 
      android:minWidth="45dp" 

Vous devriez essayer d'éviter des largeurs codées en dur et des hauteurs dans vos mises en page. Pour votre première colonne, cela ne devrait pas poser de problème, mais cela n'offre pas beaucoup de flexibilité lorsque vous définissez toutes vos largeurs sur des valeurs dp spécifiques.

+0

Ok merci je vais essayer ceci et faire un rapport – ElvisBoss

+0

Je l'ai fait dans mon xml. Je savais que tu le faisais de façon dynamique et que tu l'affichais toujours. Vous devrez trouver l'appel de la méthode pour chacun d'entre eux pour les placer dans la vue texte dynamiquement. Vous devriez regarder dans un adaptateur ListView et tableau pour obtenir ces résultats avec beaucoup moins de code. –