2013-09-23 32 views
0

已解决 - 视图元素的ID不应设置为0!如何将我的RelativeLayout从XML转换为动态编程生成的布局?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/relativeLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 


     <Button 
     android:id="@+id/Button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 1"/> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"     
     android:layout_toRightOf="@+id/Button1" /> 

    <Button 
     android:id="@+id/Button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 2" 
     android:layout_below="@+id/Button1"/> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/editText1" 
     android:layout_toRightOf="@+id/Button2"/> 

     <Button 
     android:id="@+id/Button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 2" 
     android:layout_below="@+id/Button2"/> 

    <EditText 
     android:id="@+id/editText3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/editText2" 
     android:layout_toRightOf="@+id/Button3"/> 

</RelativeLayout> 

现在是我在活动代码,我试图来的正是我在XML一样,但项目是显示完全不同的:

public class MainActivity extends Activity { 

    private RelativeLayout relativeLayout; 
    public RelativeLayout.LayoutParams layoutParams; 

    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); 


      layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT);   

      //button 1 
      Button button1 = new Button(this); 
      button1.setText("Button 1"); 
      button1.setId(0);   
      relativeLayout.addView(button1);    

      //editText1 
      EditText editText1 = new EditText(this); 
      editText1.setId(1); 
      layoutParams.addRule(RelativeLayout.RIGHT_OF, 0); 
      relativeLayout.addView(editText1, layoutParams);    
      layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT);    

      // button2 
      Button button2 = new Button(this); 
      button2.setText("Button 2"); 
      button2.setId(2); 
      layoutParams.addRule(RelativeLayout.BELOW, 0); 
      relativeLayout.addView(button2, layoutParams); 
      layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT); 

      //editText2 
      EditText editText2 = new EditText(this); 
      editText2.setId(3); 
      layoutParams.addRule(RelativeLayout.BELOW, 1); 
      layoutParams.addRule(RelativeLayout.RIGHT_OF, 2); 
      relativeLayout.addView(editText2, layoutParams); 
      layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT); 


      this.setContentView(relativeLayout);    

     } 

} 

解决: 的主要问题是,一个视图元素的ID为0是不允许的...嗯,是的,初学者的错误,我知道... 谢谢大家对这个问题的帮助!解决了!!!

+1

为什么你想避免xml布局? – Turkish

+1

我们可以通过截图了解“完全不同”的含义吗? – mithrop

回答

2

尝试this!它将xml转换为java代码。不过,我不太清楚,如果你真的需要它在这种情况下...

0

你做的所有这些倒退

relativeLayout.addView(button2, layoutParams); 
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,   ViewGroup.LayoutParams.WRAP_CONTENT); 

  应该

layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
button2.setLayoutParams(layoutParams); 
relativeLayout.addView(button2); 

您可能需要按照自己想要的方式做更多的事情,但为什么你想这样做?如果它工作,为什么改变它?

+0

没有即时通讯在每个人之前做一个新的。第一个在第一个按钮之前。因为在每个步骤中都有新的设置... – iks23

0

您已经将按钮添加到xml文件中为什么要创建动态按钮而不是夸大视图?如果要动态添加布局,当您动态地将按钮添加到相对布局时,您必须调用addRule函数来放置他们在正确的地方

只是创建按钮的对象和参照的EditText的,你有你的参考,相对布局

relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); 

并使用它们。

编辑----

有关必须从XML布局中删除按钮,然后创建按钮,像你这样做是知府只有一个这样的事在这里

<Button 
     android:id="@+id/Button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 1"/> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"     
     **android:layout_toRightOf="@+id/Button1"** /> 

放置此EDITTEXT必须使用addRule()函数

EditText editText1 = new EditText(this); 
      editText1.setId(1); 
      layoutParams.addRule(RelativeLayout.RIGHT_OF, 0); 
      relativeLayout.addView(editText1, layoutParams);    
      layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT);  

layoutParams .addRule(RelativeLayout.RIGHT_OF, 0); 

其中0是你的按钮的ID也设置布局PARAMS太布局像

editText.setLayoutParams(layoutParams); 
+0

我需要动态创建的按钮... – iks23

+0

看看我的代码,这正是我所做的,并且不起作用。 – iks23

+0

使用xml:https://docs.google.com/file/d/0B8g5mxR-PqMbV1VuOFJEVmlQakE/edit?usp=sharing 不带xml:https://docs.google.com/file/d/0B8g5mxR- PqMbQVZaZlphbEE2TEk /编辑?usp = sharing – iks23

相关问题