簡體   English   中英

動態地將 textViews 添加到一個 linearLayout

[英]Dynamically add textViews to a linearLayout

我在這里的某個地方讀到了這個,我完全失去了它,但可以使用一些幫助。

我的應用程序正在將列名從 sqlite 提取到一個數組中。 我想為每個創建一個 textview 並編輯文本(通過數組的大小),我記得在某處讀到過,您可以將 textViews 變量名稱視為數組,但我不知道現在在哪里。

那么我將如何為數組中的多個列表動態創建一個 textView 和 editText 呢?

這就像

TextView tv[] = new TextView()...

for(...){
tv[i]...
}

這是正確的嗎?

我感謝您的幫助!

像下面這樣的東西應該是你需要的:

final int N = 10; // total number of textviews to add

final TextView[] myTextViews = new TextView[N]; // create an empty array;

for (int i = 0; i < N; i++) {
    // create a new textview
    final TextView rowTextView = new TextView(this);

    // set some properties of rowTextView or something
    rowTextView.setText("This is row #" + i);

    // add the textview to the linearlayout
    myLinearLayout.addView(rowTextView);

    // save a reference to the textview for later
    myTextViews[i] = rowTextView;
}

您可以通過以下代碼在運行時添加TextView

LinearLayout lLayout = (LinearLayout) findViewById(R.id.linearlayout2); // Root ViewGroup in which you want to add textviews
for (int i = 0; i < 5; i++) {
    TextView tv = new TextView(this); // Prepare textview object programmatically
    tv.setText("Dynamic TextView" + i);
    tv.setId(i + 5);
    lLayout.addView(tv); // Add to your ViewGroup using this method
}

我認為這會很有用:

int j = 0;

context.getSystemService(Context.WINDOW_SERVICE);
WindowManager manager = (WindowManager) context
                        .getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();

for (int i = 0; i < tabsize; i++) {
    Tab tab = tabSet.get(i);
    if (i == selectedTabId)
        tab.setSelected(true);
    View view = tab.getView();

    TableRow.LayoutParams pCol = new TableRow.LayoutParams();
    pCol.width = display.getWidth() / tabSet.size();

    rowBottom.addView(view, pCol);
}

使用 ArrayList 可以幫助您動態添加任意數量的 TextView。 您甚至可能想要從父線性布局中刪除特定的 TextView。 這是一種高效的內存方式。 以下是一個片段。

ArrayList<TextView> mTextViewList = new ArrayList<>(); //empty list of TextViews

if(condition){ 
    /* can repeat several times*/

    //Create a temporary instance which will be added to the list
    final TextView mTextView = new TextView(this);

    //Add the instance to the ArrayList
    mTextViewList.add(mTextView);

    //Add view to the Parent layout in which you want to add your views
    mLinearLayout.addView(mTextView);
}

//Change the text of 6th(index:5) TextView which was added
mTextViewList.get(5).setText("My Text");

//Remove 2nd(index:1) TextView from the parent LinearLayout
mLinearLayout.removeView(mTextViewList.get(1));

對我來說,這是一個解決方案。

// 設置變量

TextView t;
ArrayList<TextView> textViewArrayList;
LayoutInflater layoutInflater;
LinearLayout ll_itensobrigatorios

// 在你的 onCreate 中告訴

layoutInflater = getLayoutInflater();
createViewItem(new String[]{"Fabio", "Santos", "Programador", "Natal"});

// 這在布局中創建視圖

    private void createViewItem(String[] nomes) {
            textViewArrayList = new ArrayList<>();

            for(int i = 0; i < nomes.length; i++) {
                View vll = layoutInflater.inflate(R.layout.nomes_tec_item, ll_itensobrigatorios, false);
                t = (TextView) vll.findViewById(R.id.txt_tec_item);
                textViewArrayList.add(t);
                ll_itensobrigatorios.addView(vll);
            }

            for(int i = 0; i < textViewArrayList.size(); i++) {
                textViewArrayList.get(i).setText((i + 1) + " - " + nomes[i]);
            }
}

假設您在 .xml 文件中創建了一個線性布局,如下所示:

<LinearLayout
    android:orientation="vertical"
    android:id="@+id/linear"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</LinearLayout>

現在動態添加 5 個文本視圖的代碼

  LinearLayout linearLayout= (LinearLayout)findViewById(R.id.linear);      //find the linear layout
    linearLayout.removeAllViews();                              //add this too
    for(int i=0; i<5;i++){          //looping to create 5 textviews

        TextView textView= new TextView(this);              //dynamically create textview
        textView.setLayoutParams(new LinearLayout.LayoutParams(             //select linearlayoutparam- set the width & height
                ViewGroup.LayoutParams.MATCH_PARENT, 48));
        textView.setGravity(Gravity.CENTER_VERTICAL);                       //set the gravity too
        textView.setText("Textview: "+i);                                    //adding text
        linearLayout.addView(textView);                                     //inflating :)
    }

在科特林

val lLayout = findViewById<View>(R.id.l1) as LinearLayout

    for (i in 0..4) {
        val tv = TextView(this)
        tv.text = "TextView $i"
        tv.id = i + 5
        lLayout.addView(tv)
    }

暫無
暫無

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

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