0

已解决 - 视图元素的 ID 不应设置为 0 !!!

<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>

现在这是我在 Activity 中的代码,我尝试完全按照我在 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 是不允许的……嗯,是的,初学者的错误,我知道……感谢任何人帮助解决这个问题!解决了!!!

4

3 回答 3

4

试试这个!它将xml转换为java代码。但是,我不太确定在这种情况下您是否真的需要它......

于 2013-09-23T10:06:26.687 回答
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);

您可能需要做更多的事情才能以您想要的方式获得它,但您为什么要这样做?如果它有效,为什么要改变它?

于 2013-09-23T10:14:12.857 回答
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 也将布局参数设置为布局,例如

editText.setLayoutParams(layoutParams );   
于 2013-09-23T10:17:13.980 回答