2017-09-15 2 views
0

アンドロイド動的テーブルレイアウトで、私は次のようにテーブルのレイアウトを作成している幅を調整することはできません:私は動的に行を作成してい私は列が

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(){ 

} 
} 

<?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をその人が持っている友人の量に依存します。

異なる携帯電話でプログラムを実行すると、表が歪んで見える可能性があるため、問題があります。どのように私は最初の列を固定することができますし、もう1つは内部の情報のサイズに応じて手配する?

これは、それがどのように見えるかです:

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と配列アダプタを調べて、これらの結果をより少ないコードで実現する必要があります。 –

関連する問題