Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将视图添加到现有布局_Android_Android Layout - Fatal编程技术网

Android 将视图添加到现有布局

Android 将视图添加到现有布局,android,android-layout,Android,Android Layout,我已经创建了一个xml布局,一切都很好。单击按钮后,我想在布局上显示更多按钮。正确的做法是什么?现在,我在同一个xml上创建按钮,并将其可见性设置为GONE。单击按钮后,将其可见性设置为可见 这是一种正确的做事方式吗?布局变得有点复杂,其他图像视图遵循相同的模式 首先非常感谢你:我不知道为什么有人给了你一个简单明了的问题 现在是答案 因此,为了正确地“制造”,整个过程应该以编程方式完成。 在我给你一些书面代码(例如)之前,我将向你解释一下你应该如何看待这个问题 你有你的主XML文件,其中有你想要

我已经创建了一个xml布局,一切都很好。单击按钮后,我想在布局上显示更多按钮。正确的做法是什么?现在,我在同一个xml上创建按钮,并将其可见性设置为GONE。单击按钮后,将其可见性设置为可见

这是一种正确的做事方式吗?布局变得有点复杂,其他图像视图遵循相同的模式


首先非常感谢你:我不知道为什么有人给了你一个简单明了的问题

现在是答案

因此,为了正确地“制造”,整个过程应该以编程方式完成。 在我给你一些书面代码(例如)之前,我将向你解释一下你应该如何看待这个问题

你有你的主XML文件,其中有你想要点击的按钮,点击后,屏幕上会出现更多的按钮。为了实现这一点,根据需要创建按钮并使其不可见或可见,这并不是一个很好的处理方法。你可能想知道为什么?这显然不是一个好方法,因为即使你的按钮是不可见的,当应用程序启动时,这些按钮,即使是不可见的,也会被创建(绘制)。这会占用空间并降低应用程序的速度

假设您希望能够在单击第一个按钮时创建数量不确定的按钮。您甚至无法通过问题中描述的方式实现这一点。您使用XML的次数太多,确实限制了您自己

解决方案:

因此,您有您的XML文件,其中有您的布局(相对的或线性的,暂时不重要)和您的按钮,按下按钮时会创建一个按钮

为了能够获得对XML布局和按钮的引用,您需要给它们一个ID。您可以在XML中这样做(我很确定您知道这一点,但我更喜欢编写完整的解释)

为布局提供ID:

   android:id="@+id/thelayout"
   android:id="@+id/button"

(If you don't know where to add those IDs, comment it,I will help further)
为布局提供ID:

   android:id="@+id/thelayout"
   android:id="@+id/button"

(If you don't know where to add those IDs, comment it,I will help further)
现在您可以参考java中的布局和按钮,这就是它的乐趣所在

您可以定义布局和按钮。注意:检查您的XML文件!!!如果你有一个RelativeLayout,你需要定义一个RelativeLayout,如果你有一个线性布局……很清楚

我假设我们有一个线性布局

 LinearLayout ll;
 Button btn;
 Button thenewbutton;

 ll= (LinearLayout)rootView.findViewById(R.id.thelayout);  //The name we gave in XML
 btn = (Button)rootView.findViewById(R.id.button);

 What we need to do now, is to add a method which will do something when we click the buttom.

 btn.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //Here we will handle the creation of the button;

            thenewbutton = new Button(getActivity()); //Created the new button
            thenewbutton.setText("One of the new buttons"); //Setted it the text in between the ""            
            setVisibility(View.VISIBLE); //Making it visible -like you were doing prolly.

            //You can customize your button via methods.Write "thenewbutton." and eclipse will show you all the methods you can use in order to "play" with the new created button.

            //Now, the button is created.All we need to do is to add it to the layout!Easy job.                

            ll.addView(thenewbutton);

            return true;
        }
    });
差不多就是这样

我已经尽可能详细地解释过了。我知道有很多东西要读,但是如果你想真正理解的话,那就花3-5分钟去读,真正地看完我写的所有东西,你就会对这个问题有另一个层次的理解

如果您需要进一步帮助,请留言


干杯

在中提取布局并显示隐藏您的包含tagOh,我忘了添加-您可以对ImageView执行完全相同的操作。非常感谢您的详细解释。你真是太好了。实际上,我在这里寻找的关键点是你在开始时所做的解释。其余的我知道,但没有考虑使用。但这对一个新手和寻找答案的人来说是件好事+1并接受:)我只是发现使用与xml类似的代码来“定位”视图比较困难。这就是全部。我想知道是否还有另一种众所周知的方法,可以在另一个xml中进行“二次”绘图层,然后将其叠加到主xml上为了从java定位视图,必须“设置LayoutParameters”。