2017-09-15 97 views
0

我已創建一個表佈局如下:我無法調整列寬的動態表格佈局的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(){ 

} 
} 

我創建動態的行所以它取決於這個人的朋友的數量。

問題是,當在不同的手機上運行程序時,表格可能看起來很好,因爲它看起來可能會失真。我怎樣才能讓第一列被固定,另一個根據信息的大小來安排?

這是它的外觀:

enter image description here

我想的第一列被固定爲3位數字,在其他列,只是調整

謝謝!

回答

0

你是指#列嗎?您正在使用這些設置創建它:

Position.setWidth(20); 

.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 

您可以刪除這些並添加這些:

  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" 

你應該儘量避免在您的佈局硬編碼的寬度和高度。對於第一列,它不應該是一個問題,但當您將所有寬度設置爲特定的dp值時,它不會提供很大的靈活性。

+0

好的,謝謝你,我會嘗試這個,並報告退回 – ElvisBoss

+0

我在我的XML做到了這一點。我知道你是在動態地做這件事,並且仍然發佈這個。您必須找到每個方法的調用,才能將它們動態設置爲textview。您應該查看一個ListView和數組適配器,以便用更少的代碼實現這些結果。 –