簡體   English   中英

如何以編程方式在線性布局中添加2個textview

[英]How to programmatically add 2 textview in a linear layout

在此輸入圖像描述

嗨,大家好,

我從API獲得以下字段“名稱”和“斯柯達”。 會有x個這樣的項目。 根據設計,我應該在下圖中顯示它們。

所以,我決定以編程方式在名為“childLayout”的線性布局中創建兩個textview,如下所示。

-- RelativeLayout

  -- Linear Layout
        -- TextView  Textview --
  -- Linear Layout 

  -- Linear Layout
        -- TextView  Textview --
  -- Linear Layout      

  -- Linear Layout
        -- TextView  Textview --
  -- Linear Layout 

--RelativeLayout

但我沒有得到理想的輸出。 請幫我解決這個問題。

這是代碼:

TextView mType;
TextView mValue;        
for (int i = 0; i < getDetailedDescAL.size(); i++) {

    LinearLayout childLayout = new LinearLayout(
            DetailedCategories.this);

    LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    childLayout.setLayoutParams(linearParams);

    mType = new TextView(DetailedCategories.this);
    mValue = new TextView(DetailedCategories.this);

    mType.setLayoutParams(new TableLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT, 1f));
    mValue.setLayoutParams(new TableLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT, 1f));

    mType.setTextSize(17);
    mType.setPadding(5, 3, 0, 3);
    mType.setTypeface(Typeface.DEFAULT_BOLD);
    mType.setGravity(Gravity.LEFT | Gravity.CENTER);

    mValue.setTextSize(16);
    mValue.setPadding(5, 3, 0, 3);
    mValue.setTypeface(null, Typeface.ITALIC);
    mValue.setGravity(Gravity.LEFT | Gravity.CENTER);

    mType.setText(getDetailedDescAL.get(i).getmPropertyType());
    mValue.setText(getDetailedDescAL.get(i).getmPropertyValue());

    childLayout.addView(mValue, 0);
    childLayout.addView(mType, 0);

    RelativeLayout.LayoutParams relativeParams = 
        new RelativeLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    relativeParams.addRule(RelativeLayout.BELOW);
    Details.addView(childLayout, relativeParams);

    // Details is the relative layout declared in XML

}

輸出是:

在此輸入圖像描述

文本視圖似乎是最重要的。 怎么解決這個問題。

替換LinearLayoutRelativeLayout並將所有TextView's添加到其中。 不要忘記LinearLayout android:orientation="vertical"

for(int j=0;j<30;j++)
            {

                 LinearLayout childLayout = new LinearLayout(
                            MainActivity.this);

                    LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT);
                    childLayout.setLayoutParams(linearParams);

                    TextView mType = new TextView(MainActivity.this);
                    TextView mValue = new TextView(MainActivity.this);

                    mType.setLayoutParams(new TableLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT, 1f));
                    mValue.setLayoutParams(new TableLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT, 1f));

                    mType.setTextSize(17);
                    mType.setPadding(5, 3, 0, 3);
                    mType.setTypeface(Typeface.DEFAULT_BOLD);
                    mType.setGravity(Gravity.LEFT | Gravity.CENTER);

                    mValue.setTextSize(16);
                    mValue.setPadding(5, 3, 0, 3);
                    mValue.setTypeface(null, Typeface.ITALIC);
                    mValue.setGravity(Gravity.LEFT | Gravity.CENTER);

                    mType.setText("111");
                    mValue.setText("111");

                    childLayout.addView(mValue, 0);
                    childLayout.addView(mType, 0);

                    linear.addView(childLayout);
            }

將您的相對布局更改為線性布局並將其解決

JFI,你為什么不簡單地使用ListView。 在帖子中,您提到項目數量未知。 因此,可能存在這樣的情況:您將擁有的項目數量超過可見屏幕空間的數量。 Listview將為您處理滾動。

PS:在這里,使用scrollView而不是Relative或LinearLayout

暫無
暫無

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

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