[英]i cannot adjust columns width in a dynamic table layout 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(){
}
}
我正在動態創建行,所以這取決於該人擁有的朋友數量。
問題在於,當在不同的手機中運行該程序時,該表看起來可能會很好,因為它看起來會變形。 我如何才能使第一列固定,而另一列根據內部信息的大小進行排列?
它是這樣的:
我希望第一列固定為3位數字,其他列僅調整
謝謝!
您是指#列嗎? 您正在使用以下設置創建它:
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值時,它並沒有提供太多的靈活性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.