带有换行文字的动态内容不起作用
Dynamic Content with wrap text not working
我想构建一个动态 table,其中 TextView
包含一些内容。文本可能很长,在这种情况下,我希望我的文本被换行。但是在下面给出的代码中它不起作用。我尝试将布局参数添加到我的 TextView
中 weight=1
不起作用。
这是我的代码:
table = FindViewById<TableLayout>(Resource.Id.main_table);
TableRow tr_head = new TableRow(this);
tr_head.Id = 10;
tr_head.SetBackgroundColor(Color.Gray);
TableRow.LayoutParams paramTableRow = new TableRow.LayoutParams(LayoutParams.MatchParent,
LayoutParams.WrapContent);
tr_head.LayoutParameters = paramTableRow;
LinearLayout.LayoutParams paramcell = new LinearLayout.LayoutParams(LayoutParams.WrapContent,LayoutParams.WrapContent,1.0f);
TextView label_Buyway = new TextView(this);
label_Buyway.Id=20;
label_Buyway.Text ="Buyway";
label_Buyway.SetTextColor(Color.Black);
label_Buyway.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_Buyway);
TextView label_qty = new TextView(this);
label_qty.Id=21;
label_qty.Text="Qty";
label_qty.SetTextColor(Color.Black);
label_qty.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_qty);
TextView label_price = new TextView(this);
label_price.Id = 22;
label_price.Text = "Price";
label_price.SetTextColor(Color.Black);
label_price.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_price);
TextView label_total = new TextView(this);
label_total.Id = 23;
label_total.Text = "Total";
label_total.SetTextColor(Color.Black);
label_total.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_total);
table.AddView(tr_head, new TableLayout.LayoutParams(
LayoutParams.MatchParent,
LayoutParams.WrapContent));
for (int i = 0; i < 10; i++)
{
string date = DateTime.Now.ToString();
Double weight_kg = 1.1;
TableRow tr = new TableRow(this);
tr.Id=(100 + i);
tr.LayoutParameters = paramTableRow;
TextView labelBuyway = new TextView(this);
labelBuyway.Id=(200 + i);
labelBuyway.Text =date;
labelBuyway.SetTextColor(Color.Black);
labelBuyway.SetBackgroundResource(Resource.Drawable.table_row);
labelBuyway.LayoutParameters = paramcell;
tr.AddView(labelBuyway);
TextView labelqty = new TextView(this);
labelqty.Id=(200 + i);
labelqty.Text=(weight_kg.ToString());
labelqty.SetTextColor(Color.Black);
labelqty.SetBackgroundResource(Resource.Drawable.table_row);
tr.AddView(labelqty);
TextView labelPrice = new TextView(this);
labelPrice.Id = (200 + i);
labelPrice.Text = (weight_kg.ToString());
labelPrice.SetTextColor(Color.Black);
labelPrice.SetBackgroundResource(Resource.Drawable.table_row);
tr.AddView(labelPrice);
TextView labelTotal = new TextView(this);
labelTotal.Id = (200 + i);
labelTotal.Text = (weight_kg.ToString());
labelTotal.SetTextColor(Color.Black);
labelTotal.SetBackgroundResource(Resource.Drawable.table_row);
tr.AddView(labelTotal);
table.AddView(tr, new TableLayout.LayoutParams(
LayoutParams.MatchParent,
LayoutParams.WrapContent));
i++;
}
现在文本被剪切并且没有换行。任何帮助将不胜感激。
为您的文本视图提供 weight=1 和 Wrap content。所以它会根据文本大小扩展。
我猜这三行就可以了:
textViewName.HorizontalScrollBarEnabled = false;
textViewName.SetMinLines(2);
textViewName.InputType = Android.Text.InputTypes.TextFlagMultiLine;
将权重 属性 设置为 1 和这三个属性,它应该可以工作。
请回复是否有效。
我想构建一个动态 table,其中 TextView
包含一些内容。文本可能很长,在这种情况下,我希望我的文本被换行。但是在下面给出的代码中它不起作用。我尝试将布局参数添加到我的 TextView
中 weight=1
不起作用。
这是我的代码:
table = FindViewById<TableLayout>(Resource.Id.main_table);
TableRow tr_head = new TableRow(this);
tr_head.Id = 10;
tr_head.SetBackgroundColor(Color.Gray);
TableRow.LayoutParams paramTableRow = new TableRow.LayoutParams(LayoutParams.MatchParent,
LayoutParams.WrapContent);
tr_head.LayoutParameters = paramTableRow;
LinearLayout.LayoutParams paramcell = new LinearLayout.LayoutParams(LayoutParams.WrapContent,LayoutParams.WrapContent,1.0f);
TextView label_Buyway = new TextView(this);
label_Buyway.Id=20;
label_Buyway.Text ="Buyway";
label_Buyway.SetTextColor(Color.Black);
label_Buyway.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_Buyway);
TextView label_qty = new TextView(this);
label_qty.Id=21;
label_qty.Text="Qty";
label_qty.SetTextColor(Color.Black);
label_qty.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_qty);
TextView label_price = new TextView(this);
label_price.Id = 22;
label_price.Text = "Price";
label_price.SetTextColor(Color.Black);
label_price.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_price);
TextView label_total = new TextView(this);
label_total.Id = 23;
label_total.Text = "Total";
label_total.SetTextColor(Color.Black);
label_total.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_total);
table.AddView(tr_head, new TableLayout.LayoutParams(
LayoutParams.MatchParent,
LayoutParams.WrapContent));
for (int i = 0; i < 10; i++)
{
string date = DateTime.Now.ToString();
Double weight_kg = 1.1;
TableRow tr = new TableRow(this);
tr.Id=(100 + i);
tr.LayoutParameters = paramTableRow;
TextView labelBuyway = new TextView(this);
labelBuyway.Id=(200 + i);
labelBuyway.Text =date;
labelBuyway.SetTextColor(Color.Black);
labelBuyway.SetBackgroundResource(Resource.Drawable.table_row);
labelBuyway.LayoutParameters = paramcell;
tr.AddView(labelBuyway);
TextView labelqty = new TextView(this);
labelqty.Id=(200 + i);
labelqty.Text=(weight_kg.ToString());
labelqty.SetTextColor(Color.Black);
labelqty.SetBackgroundResource(Resource.Drawable.table_row);
tr.AddView(labelqty);
TextView labelPrice = new TextView(this);
labelPrice.Id = (200 + i);
labelPrice.Text = (weight_kg.ToString());
labelPrice.SetTextColor(Color.Black);
labelPrice.SetBackgroundResource(Resource.Drawable.table_row);
tr.AddView(labelPrice);
TextView labelTotal = new TextView(this);
labelTotal.Id = (200 + i);
labelTotal.Text = (weight_kg.ToString());
labelTotal.SetTextColor(Color.Black);
labelTotal.SetBackgroundResource(Resource.Drawable.table_row);
tr.AddView(labelTotal);
table.AddView(tr, new TableLayout.LayoutParams(
LayoutParams.MatchParent,
LayoutParams.WrapContent));
i++;
}
现在文本被剪切并且没有换行。任何帮助将不胜感激。
为您的文本视图提供 weight=1 和 Wrap content。所以它会根据文本大小扩展。
我猜这三行就可以了:
textViewName.HorizontalScrollBarEnabled = false;
textViewName.SetMinLines(2);
textViewName.InputType = Android.Text.InputTypes.TextFlagMultiLine;
将权重 属性 设置为 1 和这三个属性,它应该可以工作。
请回复是否有效。