【问题标题】:Adding Buttons dynamically in RelativeLayout to LinearLayout将RelativeLayout中的按钮动态添加到LinearLayout
【发布时间】:2014-11-06 14:31:30
【问题描述】:

当用户输入一个单词时,他会创建一个等于单词长度的Buttons。例如:如果用户输入"aaaa",他将在第一行并排创建 4 个Buttons。然后,如果用户输入"bb",他将在第二行并排创建 2 个Buttons。而"ccc" 他创建了 3 个Buttons...

要演示的图片:

我动态创建一个RelativeLayout,然后动态地将Buttons 添加到该布局中。最后我将RelativeLayout 添加到我现有的LinearLayout 中。但问题是,每行只添加一个Button。我的程序目前看起来像这样:

谁能帮我解决这个问题?

代码:

    final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);

    final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);

    button_test.setOnClickListener(
            new View.OnClickListener()
            {
                public void onClick(View view)
                {
                    RelativeLayout relativeLayout = new RelativeLayout(view.getContext());

                    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                                    RelativeLayout.LayoutParams.WRAP_CONTENT);    


                    int size = enter_txt.getText().toString().length(); //the user input number of buttons

                    int id = 1;

                    for (int i=0; i<size; i++)
                    {
                        Button myButton = new Button(view.getContext());

                        myButton.setBackgroundResource(R.drawable.button);

                        myButton.setId(id);

                        rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());

                        relativeLayout.addView(myButton, rlp);

                        id++;
                    }

                        linearLayout.addView(relativeLayout, llp);

【问题讨论】:

    标签: android button android-linearlayout android-relativelayout


    【解决方案1】:
    rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
    

    这行说myButton应该加到myButton的右边,没有任何意义。

    解决此问题的简单方法是改用以下行

    rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId()-1);
    

    但这不是最好的方法,您应该使用具有水平方向的 LinearLayout。

    【讨论】:

    • hm,肯定还有一些错误。该程序仍然无法正常工作。我会尝试按照您的建议在 LinearLayout 中执行此操作。我只是习惯于使用垂直方向,我忘记了有一个水平方向......
    • 尝试在myButton.setId(id);行之前增加id
    【解决方案2】:

    结构要简单

    只需要在 3 种不同的线性布局中添加按钮,方向为水平。

    喜欢

    <Relative layout>{
    <LinearLayout global container with vertical orientation >{
    <LinearLayout for 'a' type buttons container with horizontal orientation>
    <LinearLayout for 'b' type buttons container with horizontal orientation>
    <LinearLayout for 'c' type buttons container with horizontal orientation>
    }
    }
    

    【讨论】:

    • 是在 xml 中吗?我选择动态编程,因为用户可以继续输入单词。不只是三个字。用户可以输入 20 个不同的单词,即 20 行按钮
    • 你需要在xml中创建布局只需要在用户输入上创建单词
    • 还有更多的词你还需要创建水平滚动视图
    【解决方案3】:

    你们是对的。使用 LinearLayout 更容易。对于那些有兴趣的人

    final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);
    
    final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
    
    button_test.setOnClickListener(
            new View.OnClickListener()
                {
                    public void onClick(View view)
                    {
                        LinearLayout linearLayout2 = new LinearLayout(view.getContext());
    
                        linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
    
                        LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
                                            LinearLayout.LayoutParams.WRAP_CONTENT,
                                            LinearLayout.LayoutParams.WRAP_CONTENT);      
    
                        int size = enter_txt.getText().toString().length();
                        for (int i=0; i<size; i++)
                        {
                            Button myButton = new Button(view.getContext());
                            myButton.setBackgroundResource(R.drawable.button);
                            linearLayout2.addView(myButton, rlp);
                         }
                         linearLayout.addView(linearLayout2, llp);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多