如何使用getResource.getIdentifier()来获取布局? [英] How to use getResource.getIdentifier() to get Layout?

查看:783
本文介绍了如何使用getResource.getIdentifier()来获取布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,每个人。​​

I have a question,everyone.

我想用getResource.getIdentifier()得到的XML我的布局元素(布局/ xxx.xml)。

I want to use getResource.getIdentifier() to get my layout element in XML (layout/xxx.xml).

但我遭遇资源ID#0x7f070003型#0×12是无效...错误消息。

But I am encounter " Resource ID #0x7f070003 type #0x12 is not valid..." error message.

我不kwnow如何修复这个问题。

I don't kwnow how to fixed this problem.

我是用一个方法返回的LinearLayout。

I am use a method return linearlayout.

这是我下面的code:

It's my code below:

public class MainActivity extends Activity {


 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  getWeekOfDay(2).setBackgroundColor(Color.RED);
 }

 private LinearLayout getWeekOfDay(int n)
 {
  int layoutID = getResources().getIdentifier("layout"+n, "id", getPackageName());
  return (LinearLayout) LayoutInflater.from(this).inflate(layoutID, null);
 }
....
...
..
}

我的设计有很多的LinearLayout,但名称相似(布局1,布局2,layout3 ...)。

My layout have many linearlayout, but the name is similar(layout1, layout2, layout3...).

我要使用调用方法来获取指定的元素。

I want to use call method get appoint element.

我的布局code是如下:

my layout code is below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.37"
        android:background="#cccccc" >

            <TextView
            android:id="@+id/TextView03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test1" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.37"
        android:background="#cccccc" >

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test2" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.37"
        android:background="#cccccc" >

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test3" />
</LinearLayout>

<LinearLayout
    android:id="@+id/layout4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.37"
    android:background="#cccccc" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test4" />

</LinearLayout>

但Java code将返回错误。

But the java code will return error .

该错误消息如下:

      : FATAL EXCEPTION: main
     : java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testdemo/com.example.testdemo.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f070003 type #0x12 is not valid
     :  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
     :  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
     :  at android.app.ActivityThread.access$600(ActivityThread.java:139)
     :  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
     :  at android.os.Handler.dispatchMessage(Handler.java:99)
     :  at android.os.Looper.loop(Looper.java:154)
     :  at android.app.ActivityThread.main(ActivityThread.java:4945)
     :  at java.lang.reflect.Method.invokeNative(Native Method)
     :  at java.lang.reflect.Method.invoke(Method.java:511)
     :  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
     :  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
     :  at dalvik.system.NativeStart.main(Native Method)
     : Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f070003 type #0x12 is not valid
     :  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2358)
     :  at android.content.res.Resources.getLayout(Resources.java:952)
     :  at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
     :  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
     :  at com.example.testdemo.MainActivity.getWeekOfDay(MainActivity.java:25)
     :  at com.example.testdemo.MainActivity.onCreate(MainActivity.java:19)
     :  at android.app.Activity.performCreate(Activity.java:4531)
     :  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
     :  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
     :  ... 11 more

有任何人知道这个问题?如何解决这个问题呢?

Have anyone know this problem? How to resolve this problem?

非常感谢你。 :)

推荐答案

通过这样的:

int layoutID = getResources().getIdentifier("layout"+n, "layout", getPackageName());

你基本上检索可以膨胀的布局文件的ID。它的动态版本

you basically retrieve the id of a layout file that you can inflate. It's the dynamic version of

int layoutID = R.layout.layout1;

您打算做的是检索一个已经膨胀布局的视图。这就是你如何做到这一点:

What you intend to do is retrieve a view from an already inflated layout. That's how you'd do it:

int layoutID = getResources().getIdentifier("layout"+n, "id", getPackageName());
return (LinearLayout)findViewById(layoutID);

这就是动态版

That's the dynamic version of

return (LinearLayout)findViewById(R.id.layout1);

这篇关于如何使用getResource.getIdentifier()来获取布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆