【问题标题】:How create LinearLayout dynamically with aligned elements如何使用对齐的元素动态创建 LinearLayout
【发布时间】:2021-10-21 12:08:55
【问题描述】:

我是 android 的新手。需要在 RadioGroup 内动态创建 LinearLayout 并对齐 TextViews,如下所示。在这里,我只是从调色板中移动了元素,并没有更改任何属性

但是得到这个

我错过了什么?

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, /* or MATCH_PARENT */
      LinearLayout.LayoutParams.WRAP_CONTENT, /* or MATCH_PARENT */
      1.0f);

        final LinearLayout linearLayout = new LinearLayout(this);

        final TextView text1 = new TextView(this);
        text1.setText("aaa");
        text1.setLayoutParams(param);

        linearLayout.addView(text1);

        final TextView text2 = new TextView(this);
        text2.setText("bbb");
        text2.setLayoutParams(param);

        linearLayout.addView(text2);

        final TextView text3 = new TextView(this);
        text3.setText("ccc");
        text3.setLayoutParams(param);

        linearLayout.addView(text3);
        radioGroup.addView(linearLayout);

【问题讨论】:

    标签: android dynamic android-linearlayout


    【解决方案1】:

    LayoutParams 应该同时是 MATCH_PARENT 并且 linearLayout 也应该设置为该参数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 2023-03-27
      • 2012-12-11
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多