1

我正在尝试在 Visual Studio 的 Monodroid 应用程序中实现 .java 启动画面,无论如何我希望启动画面从资源布局中获取它的内容视图。我试图这样得到它:

setContentView(R.layout.AppSplash);

还尝试过:

setContentView(Resource.layout.AppSplash);

并且:

setContentView("@layout/AppSplash");

我收到这样的错误消息:

package R does not exist

其中 R 更改资源或:

cannot find symbol
symbol  : method setContentView(java.lang.String)
location: class SwimmerTimesCalc.SplashActivity
    setContentView("@layout/AppSplash");

当我尝试 @layout/AppSplash 选项时

如何访问 Monodroid 资源来设置启动画面的布局?

4

3 回答 3

1

应该可以帮助您入门。

如果您想使用专门用于创建自己的启动屏幕的布局,然后使用该资源在您的活动中显示它,那么您可以使用类似这样的东西。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/SplashScreenLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<ImageView
    android:id="@+id/SplashDefault"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
    android:scaleType="centerCrop" />
</RelativeLayout>

然后在您的活动中,您可以设置内容视图。

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.SplashLayout);
    }
于 2012-08-21T16:23:10.117 回答
0

我想出了如何访问资源,因为调试器无法访问资源我去了生成的资源文件,该文件位于 \obj[This can be debug or release]\android\src 下的项目文件夹中,它被称为 R。虽然浏览该文件我发现我试图使用这样的布局:

public static final int appsplash1=0x7f030002;

我从那里获取资源值 0x7f030002 并像这样使用它:

setContentView(0x7f030002);

无论如何,由于资源文件是自动生成的,因此在此之前添加另一个按字母顺序排列的布局可能需要再次执行此过程。

于 2012-08-23T18:58:41.173 回答
0

这里有同样的问题。可以通过它的名称和文件夹在运行时找到资源的 id,如下所示:

int iconResourceId = context.getResources().getIdentifier("icon", "drawable", context.getPackageName());

因此,在 Java 文件中,可以使用这样的查找而不是 R.drawable.icon 并且它会起作用。

然而,这比较慢(因为查找在 Android 上的实现效率低下)并且仍然会将资源的名称硬编码为字符串。如果在 Mono 项目中移动或重命名资源,Java 文件不会知道这一点。

此外,必须以小写形式键入所有资源名称,因为 Mono 将名称从 .NET 样式(如“Icon.png”)转换为 Android 样式“icon.png”。

于 2013-03-08T13:08:10.207 回答