android - 带有换行文本的动态内容不起作用

标签 android xamarin.android tablelayout

我想构建一个动态表格,其中包含带有一些内容的 TextView 。文本可能很长,在这种情况下,我希望文本被换行。但在下面给出的代码中它不起作用。我尝试使用 weight=1 将布局参数添加到我的 TextView 中,但不起作用。

这是我的代码:

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++;
}

现在文本被剪切并且不换行。任何帮助将不胜感激。

最佳答案

我猜这三行就可以解决问题:

   textViewName.HorizontalScrollBarEnabled = false;   
   textViewName.SetMinLines(2);
   textViewName.InputType = Android.Text.InputTypes.TextFlagMultiLine;

将权重属性设置为 1 和这三个属性,它应该可以工作。

请恢复是否有效。

关于android - 带有换行文本的动态内容不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47526196/

相关文章:

android - Sqlite数据库查询编写

xamarin - Android Google Analytics 无法实例化接收器 com.google.analytics.tracking.android.CampaignTrackingReceiver

html - 溢出切断效果:hidden

android - 如何在表格布局的底部显示按钮?

android - 尽管使用 ellipsize 属性,但 TextView 不会在 TableLayout 中被截断

java - 无法从 PC 找到 Android 应用程序创建的文件夹

android - GCM XMPP CCS 与 MQTT

android - 在 Nokia 6.2 Android 9 上找不到处理 Intent android.intent.action.OPEN_DOCUMENT_TREE 的 Activity

xamarin - 在 xamarin.forms 中应用动画

c# - 更新后 Xamarin Forms 部署失败