【问题标题】:Switch Between views in FrameLayout from a child view从子视图在 FrameLayout 中的视图之间切换
【发布时间】:2011-05-08 14:58:42
【问题描述】:

我目前有一个存储在 FrameLayout 中的 SurfaceView(名为 BoardView)。在此 FrameLayout(f1) 中存储了另一个 LinearLayout (l1),其中包含一个 EditText。我希望能够从我的 BoardView 中将 EditText 放在前面。这可能吗?我尝试使用 getParent().bringChildToFront();但它没有用。有什么想法吗?

public class Board extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    //use a frame layout so you can also display a dialog box
    // allows more than one view to be used
    FrameLayout f1 = new FrameLayout(this);
    LinearLayout l1 = new LinearLayout(this);
    EditText edit = new EditText(this);

    l1.setOrientation(LinearLayout.VERTICAL);
    l1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT));
    edit.setText("Enter your name!");
    l1.addView(edit);

    f1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
            LayoutParams.FILL_PARENT));
    f1.addView(l1);
    f1.addView(new BoardView(this));

    setContentView(f1);

    //setContentView(new BoardView(this));
}

}

【问题讨论】:

    标签: android android-edittext android-linearlayout surfaceview android-framelayout


    【解决方案1】:

    听起来很傻,但试试l1.bringToFront() 看看是否可行?或者,只需添加 l1 秒:

    f1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
                LayoutParams.FILL_PARENT));
        f1.addView(new BoardView(this));
        f1.addView(l1);
    

    编辑文本将在顶部。

    让我知道它是否有效。

    【讨论】:

    • 问题是,它说“l1”无法解析。我只想在 SurfaceView 发生某些事情时显示 l1。基本上,当游戏结束时,我想展示这个新视图。我想我可以让当前视图不可见,对吧?
    • 啊,这就是问题所在:显示您进行切换的代码
    • 好吧,这段代码真的很长。我有一个自定义 SurfaceView - 没有足够的空间在这里发布代码:/ 我应该怎么做才能给你看?我的班级标题看起来像这样。公共类 BoardView 扩展 SurfaceView 实现 SurfaceHolder.Callback{
    • 是自定义类有问题吗?肯定有办法做到这一点。
    • 应该没关系。无法解决的问题只是因为您的自定义类没有 l1 对象的可见性。我建议将 ID 分配给 l1,然后使用 id:在上面的 onCreate 中使用 l1.setId(1);,然后在您的自定义类中使用 ((View)getParent()).findViewById(1).bringToFront(); 来更新 z 顺序。
    猜你喜欢
    • 1970-01-01
    • 2019-04-10
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多