【问题标题】:Building xml relative layout programmatically以编程方式构建 xml 相对布局
【发布时间】:2013-04-24 12:51:05
【问题描述】:

我正在尝试在运行时更新/填充 xml。 textViews 显示得很好,但似乎无法在第一项之后正确定位它们(​​参见 else 语句)。是因为getId() 无法识别还是我完全错了?

for(int x=1; x<13; x++){
    String prompt="PROMPT_"+String.valueOf(x);
    String promptValue = myTacorCursor.getString(myTacorCursor.getColumnIndex(prompt));
    //if not empty draw a row
    if (!promptValue.equals("")){
        //insert new rows into layout
        RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.RelativeLayout1);
        TextView promptLabel = new TextView(this);
        promptLabel.setTextAppearance(this, android.R.style.TextAppearance_DeviceDefault_Large);
        promptLabel.setText(myTacorCursor.getString(myTacorCursor.getColumnIndex("PROMPT_"+String.valueOf(x))));                
        promptLabel.setId(1);
        ((RelativeLayout) myLayout).addView(promptLabel);

        RelativeLayout.LayoutParams mLayoutParams1=(RelativeLayout.LayoutParams)promptLabel.getLayoutParams();
        mLayoutParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        if (i==1){
            mLayoutParams1.addRule(RelativeLayout.BELOW,R.id.textView7);
            Log.w("ID is:", String.valueOf(promptLabel.getId()));
        } else{                 
            mLayoutParams1.addRule(RelativeLayout.BELOW,promptLabel.getId());
            Log.w("ID is:", String.valueOf(promptLabel.getId()));
        }
    i++;
    }
}   

我正在尝试显示:

(textView)LABEL xx R.id.textview7
<-- here would be the inserted columns -->
(text view) prompt 1 
(text view) prompt 2
(text view) prompt 3
... etc ...'

【问题讨论】:

  • 如果您说明要在屏幕上显示的内容,将会很有用。

标签: android xml android-relativelayout


【解决方案1】:

动态设置 id 是可以的。只需多加注意,您的代码就可以工作。

  1. 就您在for 循环中而言,最好只在一处增加索引。
  2. 更改ViewLayoutParams 后,您需要将其重新设置:promptLabel.setLayoutParams(layoutParams)

试试这个。它应该工作:

public class MainActivity extends Activity {

    private static final int BASE_ID = 1;
    private static final int ITEMS_COUNT = 13;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.root);

        for (int index = BASE_ID; index < ITEMS_COUNT; index++) {
            String prompt = "PROMPT_" + String.valueOf(index);
            // if not empty draw a row
            // insert new rows into layout
            TextView promptLabel = new TextView(this);
            promptLabel.setTextAppearance(this,
                    android.R.style.TextAppearance_DeviceDefault_Large);
            promptLabel.setText(prompt);
            promptLabel.setId(index);
            rootLayout.addView(promptLabel);

            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) promptLabel
                    .getLayoutParams();
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            if (index == BASE_ID) {
                layoutParams.addRule(RelativeLayout.BELOW, R.id.top);
                Log.d("ID is:", String.valueOf(index));
            } else {
                layoutParams.addRule(RelativeLayout.BELOW, index - 1);
                Log.d("ID is:", String.valueOf(index - 1));
            }
            promptLabel.setLayoutParams(layoutParams);
        }
    }
}

【讨论】:

  • 完美运行!现在一切都说得通了。
【解决方案2】:

您不需要以编程方式构建整个布局。在单独的布局xml 文件中定义它,然后使用布局充气器对其进行充气。之后将其添加到您想要的位置。

我从未见过ids 以编程方式分配,但根据我的建议,您可以像往常一样在 xml 中定义它们。

PS:Here 是一个很好的例子来解释如何使用LayoutInflater

【讨论】:

  • 我可以看到你来自哪里,我已经尝试实现它,但我遇到了和以前一样的问题,因为我希望它尽可能动态。问题:如果我使用 LayoutInflater,我是否能够即时覆盖 textView 的 ID,或者我必须在 xml 中创建 13 个 textView 实例,因为可能有 1、2 或 13 个提示。
  • @user2224135 为什么你的目标是使用 id 而不是标签?我知道您可以以编程方式控制标签。
  • 这是为了更好的控制和数据处理,因为我有许多可能的布局变化。请参阅解决问题的 riwnodennyk 答案。你建议的想法也有效
猜你喜欢
  • 1970-01-01
  • 2018-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-06
  • 1970-01-01
  • 2013-07-05
相关资源
最近更新 更多