【问题标题】:Android: Add programmatically multiple views to linear layoutAndroid:以编程方式将多个视图添加到线性布局
【发布时间】:2013-02-18 15:15:41
【问题描述】:

我不确定如何处理。我需要线性布局,它需要有 20 多个 TextView 和 EditText 组件。我可以在顶部定义 ImageView 和按钮,但我不知道如何生成下面的其余组件,然后在最后放置一个按钮。

它看起来像这样:

---Button-----Button-------

--------ImageView----------

TextView -------- EditText

Item1.............[-------]

Item2.............[-------]

Item3.............[-------]

Item4.............[-------]

.

.

.

------------Button------------

确实会有很多组件,所以我想避免在 XML 中手动定义所有组件。单击按钮后,所有组件都需要成对保存“名称”:“值”(“Item1”:“EditText 值”)。我有一个项目名称列表,用户将写入这些项目的值并将它们保存到 JSON 文件。

提前谢谢你。

【问题讨论】:

  • 您应该使用带有自定义适配器的 ListView
  • 谢谢。这是个好主意,但我的布局有点问题。这些按钮、文本输入字段和图像占用了大量空间,所以我需要将所有这些都放在ScrollView 中。问题是现在我不能把ListView 放在ScrollView 里面。 XML 中的警告:The vertically scrolling ScrollView should not contain another vertically scrolling widget (ListView)

标签: android android-edittext android-linearlayout android-view dynamically-generated


【解决方案1】:

你必须实现一个这样的类。

public class DetailedTicketSystemView extends LinearLayout 
{
    static int inc = 100;
    ArrayList<XmlScommessaInCorso> _viewData;
    ArrayList<xmlSistemaMovimenti> _systems;
    Context _context;

    public DetailedTicketSystemView(Context context, int res, AttributeSet attrs, ArrayList<XmlScommessaInCorso> viewData, ArrayList<xmlSistemaMovimenti> systems) 
    {
        super(context, attrs);

        _context = context;
        _viewData = viewData;
        _systems = systems;

        LayoutInflater.from(context).inflate(res, this, true);
        setId(inc);
        inc++;

        setLayout();
    }

    private void setLayout()
    {
        TextView textView;
        String text;

        for (int i = 0; i < _systems.size(); i++)
        {       
            View quotesView = (View) LayoutInflater.from(_context).inflate(R.layout.row_ticket_detail_system_layout, null, true);
            quotesView.setId(i);

            // System Id
            textView = (TextView) quotesView.findViewById(R.id.systemId);
            text = "" + _context.getString(R.string.id_sistema) + " " + _systems.get(i).getAttributeOrVoidString(xmlSistemaMovimenti.ATTR_id) +
                    " / " + _viewData.size();
            textView.setText(text);

            // System Columns
            textView = (TextView) quotesView.findViewById(R.id.systemColumns);
            text = "" + _context.getString(R.string.colonne) + " " + _systems.get(i).getAttributeOrVoidString(xmlSistemaMovimenti.ATTR_n_multiple_sis);
            textView.setText(text);

            // System Bet Text
            textView = (TextView) quotesView.findViewById(R.id.systemBetText);
            textView.setText(R.string.importoPerColonna);

            // System bet Value
            textView = (TextView) quotesView.findViewById(R.id.systemBetValue);
            text = "€ " + XmlScommessaInCorso.getQuotaFormattedOrVoid(_systems.get(i).getAttributeOrVoidString(xmlSistemaMovimenti.ATTR_impo_sistema));
            textView.setText(text);


            LinearLayout primaryLayout = (LinearLayout) findViewById(R.id.primaryLayout);
            primaryLayout.addView(quotesView);
        }
    }
}

【讨论】:

  • 不知道你是只需要自定义类还是列表视图。
  • 谢谢。如果 ListView 不成功,我会尝试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-02
  • 1970-01-01
相关资源
最近更新 更多