如何在 Android 中从另一个视图膨胀另一个视图

发布于 2024-11-29 02:23:18 字数 293 浏览 1 评论 0原文

我知道在我的 onCreate() 中,我可以通过以下方式从 XML 中扩充视图:

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

但是我怎样才能从另一个视图中做到这一点呢?我试图通过将其可见性从 GONE 设置为 VISIBLE 来调用加载屏幕,但似乎无法从我的 glSurfaceView 中找出如何执行此操作

I know in my onCreate() I can inflate a view from XML by something like:

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

But how could I do this from another view? Im trying to call up a loading screen by setting its visibility from GONE to VISIBLE but cant seem to figure out how to do this from my glSurfaceView

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葬花如无物 2024-12-06 02:23:18

如果你想膨胀一个布局,代码如下所示:

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myRoot = new LinearLayout(context);
View itemView = inflater.inflate(R.layout.layout_details, myRoot);

在这里,你首先创建一个新的 LinearLayout,然后将 id 为 R.layout.layout_details 的布局膨胀到其中。然后,inflate 方法返回 myRoot 视图。

这里有一个关于 LayoutInflater 的教程:

If you want to inflate a layout the code looks like this:

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myRoot = new LinearLayout(context);
View itemView = inflater.inflate(R.layout.layout_details, myRoot);

Here you first create a new LinearLayout an then inflate the layout with id R.layout.layout_details into it. The inflate method then returns the myRoot view.

Here is a tutorial about the LayoutInflater:

仅此而已 2024-12-06 02:23:18

那其实并不是充气。膨胀是解析 XML 布局文件并从中创建 View 和 ViewGroup 类实例结构的过程(例如,setContentView() 在后台为您完成此操作)。

您所做的就是在 XML 布局文件中定义的代码中获取对视图的引用。要更改 GLSurfaceView 的可见性,您必须像上面那样引用它。但请记住,视图(在本例中为 GLSurfaceView)必须在布局文件中定义。

引用后,您必须调用 GLSurfaceView.setVisibility() 来更改其可见性。

这是一个示例:

GLSurfaceView glsurface = (GLSurfaceView) findViewById(R.id.myglsurfaceid);
glsurface.setVisibility(View.VISIBLE);

当然,您可以使用 View.INVISIBLEView.GONE ,具体取决于您想要执行的操作。

如果您引用一个布局(例如RelativeLayout),您可能会使用您的RelativeLayout实例的findViewById()找到该布局的子级:

RelativeLayour rl = (RelativeLayout) findViewById(R.id.mylayout);
(Button) mybutton = (Button) rl.findViewById(R.id.mybutton);

但这通常是不必要的(至少当您刚开始使用Android时) )因为活动 findViewById() 会查找所有显示的视图,即使在子布局中也是如此。仅当您的 ui 结构中有重复的 id(老实说,我还没有遇到过这种情况)并且想要指定在哪里查找特定视图时,您才需要使用它。

Thats actually not inflating. Inflating is the process that parses a XML layout file and creates a structure of View and ViewGroup class instances out of it (setContentView() does this for you in the background for example).

What you do is getting a reference to a view in code that you have defined in your XML layout file. To change the visibility of your GLSurfaceView you have to reference it like you did above. But remember that the View (GLSurfaceView in this case) has to be defined in your layout file.

After referencing you have to call GLSurfaceView.setVisibility() to change it's visibility.

Here's an example:

GLSurfaceView glsurface = (GLSurfaceView) findViewById(R.id.myglsurfaceid);
glsurface.setVisibility(View.VISIBLE);

Of course you can use View.INVISIBLE or View.GONE either, depending on what you want to do.

If you reference a layout (such as a RelativeLayout), you may find children of this layout with the findViewById() of your RelativeLayout instance:

RelativeLayour rl = (RelativeLayout) findViewById(R.id.mylayout);
(Button) mybutton = (Button) rl.findViewById(R.id.mybutton);

But thats usually not neccessary (at least when you just started with Android) because the activities findViewById() finds all Views that are displayed, even in sublayouts. You only have to use it if you have duplicate ids in your ui structure (tbh I never had that case yet) and want to specifiy where to look for your particular View.

遮了一弯 2024-12-06 02:23:18

您无法获取对当前 Layout 或当前 View 中不存在的 View 的引用(您当前的< code>Activity content) ,但您可以使用当前 中的 LayoutInflater 从另一个 XML 布局创建新的 View活动


您可以向当前的Activity内容添加一个新的View,这就是您提到的“加载屏幕”,甚至可以将其显示为Dialog > 或者通过创建 View 然后将其添加到您的 Activity 中的根布局

我希望对您有帮助

You can't get a reference to a View that's doesn't exists in your current Layout, or your current View, (your current Activity content) , but you can create a new View from another XML layout, using LayoutInflater from current Activity.


you can add to you current Activity content, a new View, that's what you mentioned as " loading screen ", even by showing it as a Dialog or by creating View and then add it to root layout in your Activity

I hope I helped you

九公里浅绿 2024-12-06 02:23:18

如果我正确理解你想要做什么:

假设你有一个 glSurfaceView 对象并且你想获取该对象内部的视图。
您将执行与在正常视图中执行的操作相同的操作。让我们说一个按钮:

Button button = (Button) glSurfaceView.findViewById(R.id.buttonid);

如果您有不同的意思,请在评论中告诉我。

编辑:然后您可以设置按钮的可见性:

button.setVisibility(Button.GONE)

If I correctly understood what you wanna do:

Supposing you have a glSurfaceView object and you wanna grab a view that's inside that one.
You'll do just the same thing you did for you normal view. Let's say a button:

Button button = (Button) glSurfaceView.findViewById(R.id.buttonid);

If you meant something different let me know in the comments.

EDIT: And then you can just set the button's visibility:

button.setVisibility(Button.GONE)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文