我无法在动态表布局android

时间:2017-09-15 20:10:04

标签: android android-tablelayout

我创建了一个表格布局如下:

<?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位,其他列只需调整

谢谢!

1 个答案:

答案 0 :(得分: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值时,它不会提供很大的灵活性。