【问题标题】:Is it possible to add buttons to a framelayout that was set programmatically?是否可以将按钮添加到以编程方式设置的框架布局中?
【发布时间】:2013-07-29 18:08:58
【问题描述】:

这有点难以描述,但我会尽力而为:

我正在开发一个使用自定义相机活动的 android 应用程序。在此相机活动中,我使用以编程方式创建表面视图并将其设置为在 xml 布局文件中定义的框架布局(覆盖全屏)。

我现在的问题是,如何在框架布局中添加其他元素?仅以编程方式?我问是因为到目前为止我只能以编程方式添加其他元素。我在 xml 布局中添加的元素没有出现在屏幕上。 它们是否可能就在我添加到框架布局的表面视图后面?如果是这样,是否有可能将他们带到前线?

谢谢你们!

【问题讨论】:

    标签: java android android-linearlayout android-framelayout


    【解决方案1】:

    当然,您可以在FrameLayout 中添加尽可能多的按钮和其他小部件。由于FrameLayout 允许堆叠视图,因此您在 xml 文件中添加的组件现在位于您以编程方式添加的视图后面。以下是动态创建和添加小部件的方法:

    // find your framelayout
    frameLayout = (FrameLayout) findViewById(....);
    
    // add these after setting up the camera view        
    
    // create a new Button
    Button button1 = new Button(this);
    
    // set button text
    button1.setText("....");
    
    // set gravity for text within button
    button1.setGravity(Gravity.....);
    
    // set button background
    button1.setBackground(getResources().getDrawable(R.drawable.....));
    
    // set an OnClickListener for the button
    button1.setOnClickListener(new OnClickListener() {....})
    
    // declare and initialize LayoutParams for the framelayout
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
    
    // decide upon the positioning of the button //
    // you will likely need to use the screen size to position the
    // button anywhere other than the four corners
    params.setMargins(.., .., .., ..);
    
    // use static constants from the Gravity class
    params.gravity = Gravity.CENTER_HORIZONTAL;
    
    // add the view
    fl1.addView(button2, params);
    
    // create and add more widgets
    
    ....
    ....
    

    编辑 1:

    你可以在这里使用一个技巧:

    // Let's say you define an imageview in your layout xml file. Find it in code:
    imageView1 = (ImageView) findViewById(....);
    
    // Now you add your camera view.
    .........
    
    // Once you add your camera view to the framelayout, the imageview will be 
    // behind the frame. Do the following:
    framelayout.removeView(imageView1);
    framelayout.addView(imageView1);
    
    // That's it. imageView1 will be on top of the camera view, positioned the way
    // you defined in xml file
    

    发生这种情况是因为:

    子视图在堆栈中绘制,最近添加的子视图在顶部(来自 FrameLayout 上的 android 资源页面)

    【讨论】:

    • 那么你会说动态添加元素是唯一的方法吗?
    • @user2426316 否。参见上面的编辑 1
    【解决方案2】:

    http://developer.android.com/reference/android/widget/FrameLayout.html

    “FameLayout 被设计为在屏幕上屏蔽一个区域以显示单个项目。通常,应该使用 FrameLayout 来保存单个子视图,因为很难以可扩展到不同的方式来组织子视图屏幕尺寸没有子项相互重叠。但是,您可以将多个子项添加到 FrameLayout 并通过使用 android:layout_gravity 属性为每个子项分配重力来控制它们在 FrameLayout 中的位置。"

    【讨论】:

    • android:layout_gravity 属性对我没有帮助。我的框架布局中仍然不能有多个元素。
    • 建议不要在 Framelayout 上使用多个项目。您还想反对该建议吗?
    猜你喜欢
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 2014-03-30
    • 1970-01-01
    • 2020-10-22
    • 2011-11-03
    相关资源
    最近更新 更多