簡體   English   中英

自定義帶有邊框的Android TextView,並使用for循環創建多個邊框?

[英]Custom Android TextView with a border, and create multiple using for-loop?

因此,我想創建一個自定義的Android TextView,並在XML文件中添加邊框(而不是通過編程),並使用for循環創建其中的10個。 像這樣:

LinearLayout layout = new LinearLayout(//something, //something); 
TextView tv;
String[] data = //Certain data which I'm getting

for(int i = 0; i < data.length; i++) { 
tv = (TextView) findViewById(R.id.tvTest); 
layout.addView(tv); 
} 

但這出於某種原因對我不起作用。 總結一下:

  • >用XML創建自定義TextView(不是Java編程,而是XML)
  • >創建此特定TextView的x數量
  • >將其添加到Java屏幕中

你能幫我嗎?

這是代碼片段

 LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    for (  ) {
      View listItemView = inflater.inflate(R.layout.detaillistitem, _ListLayout, false);
      _ListLayout.addView(listItemView);

      TextView descriptionText = (TextView) listItemView.findViewById(R.id.Text1);
      descriptionText.setText("");

    }

R.layout.detaillistitem是您要充氣的物品。 _ListLayout是LinearLayout,您可以在上面添加項目。

像這樣嘗試

在外部布局文件中創建textview。

for(int i = 0; i < data.length; i++) { 
View v = LayoutInflater.from(this).inflate(R.layout.textview, null);//R.layout.textview is your textview id you want to repeat..
layout.addView(v);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM