按ID

时间:2016-02-20 19:31:06

标签: android

我在获取布局ID时遇到问题。

splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splash_img1" >

</LinearLayout>

.java文件

@Override
protected void onCreate(Bundle savedStateInstance) {
    super.onCreate(savedStateInstance);
    setContentView(R.layout.splash);

    int imgId[] = new int[] { R.drawable.splash_img1, R.drawable.splash_img2 };
    Random random = new Random();
    int result = random.nextInt(imgId.length);
    LinearLayout layout = (LinearLayout) findViewById(R.layout.splash);
    layout.setBackgroundDrawable(getResources().getDrawable(imgId[result]));

}

每当我运行它时,我得到一个Gradle构建错误&#34;未定义的布局&#34;。

1 个答案:

答案 0 :(得分:2)

您可以UiLoader获取layout

在您的R.id.layout_id文件中添加ID

layout

将此添加到您的<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/splash_layout" // <-- this to your code. android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/splash_img1" > </LinearLayout> 文件

.java

希望这会有所帮助:)

相关问题