动态地将子项添加到LinearLayout并获取每个孩子的位置

时间:2011-07-12 09:02:06

标签: android position dynamic android-linearlayout

我在获取孩子的 LinearLayout 的位置时遇到问题。首先,我动态添加了一些按钮,然后我尝试返回每个孩子的索引并将其显示到 TextView 中。我在这里分享代码:

java源

private String[] categories;

private LinearLayout ll;
private TextView tv;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    categories = getResources().getStringArray(R.array.categories);

    tv = (TextView) findViewById(R.id.text);
    ll = (LinearLayout) findViewById(R.id.hsvLinearLayout);

    for(int i = 0; i < categories.length; i++) {
        Button btn = new Button(this);
        btn.setText(categories[i]);
        btn.setOnClickListener(buttonClick);
        ll.addView(btn);
    }
}

OnClickListener buttonClick = new OnClickListener() {
    public void onClick(View v) {
        tv.setText(ll.indexOfChild(v));
    }
};

xml结构

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<HorizontalScrollView
    android:id="@+id/Footer"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="none"
    android:fadingEdge="none"
    >

    <LinearLayout
        android:id="@+id/hsvLinearLayout"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

    </LinearLayout>

</HorizontalScrollView>

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

资源

<string name="today">Today</string>
<string name="life">Life</string>
<string name="corner">Corner</string>
<string name="banks">Banks</string>
<string name="it">IT</string>
<string name="fun">Fun</string>

<array name="categories">
    <item>@string/today</item>
    <item>@string/life</item>
    <item>@string/corner</item>
    <item>@string/banks</item>
    <item>@string/it</item>
    <item>@string/fun</item>
</array>

动态添加很好,但我设置 OnClickListener 的方式会产生一些错误。任何帮助都会有用!这样做的目的是,如果我想在 Horizo​​ntalScrollView 中添加一个或多个按钮,则无需编辑多个文件,只需转到 sting.xml 并在 categories-array 中创建一个新项目!

LogCat 生成的内容:

07-12 22:37:17.680: INFO/System.out(331): waiting for debugger to settle...
07-12 22:37:17.900: INFO/System.out(331): debugger has settled (1441)
07-12 22:37:20.870: INFO/ActivityManager(61): Displayed activity com.test/.TestDynam: 10203 ms (total 10203 ms)
07-12 22:37:25.552: WARN/ResourceType(331): No package identifier when getting value for resource number 0x00000000
07-12 22:37:26.871: DEBUG/dalvikvm(132): GC freed 190 objects / 8976 bytes in 901ms

澄清一下 - 应用程序启动没有任何错误,但是当我点击一个按钮时,它会从上面的日志中生成这一行:

07-12 22:37:25.552: WARN/ResourceType(331): No package identifier when getting value for resource number 0x00000000

这是来自调试器

TestDynam [Android Application] 
    DalvikVM[localhost:8611]    
        Thread [<3> main] (Suspended (exception Resources$NotFoundException))   
            ViewRoot.handleMessage(Message) line: 1704  
            ViewRoot(Handler).dispatchMessage(Message) line: 99 
            Looper.loop() line: 123 
            ActivityThread.main(String[]) line: 4203    
            Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
            Method.invoke(Object, Object...) line: 521  
            ZygoteInit$MethodAndArgsCaller.run() line: 791  
            ZygoteInit.main(String[]) line: 549 
            NativeStart.main(String[]) line: not available [native method]  
        Thread [<13> Binder Thread #2] (Running)    
        Thread [<11> Binder Thread #1] (Running)

2 个答案:

答案 0 :(得分:14)

你的问题是资源.... 你使用setText(int)方法(索引是int ...)来寻找资源而不是字符串,这不是StringBuilder,你可以为它抛出任何类型并获得一个字符串。 你需要更换

  

tv.setText(ll.indexOfChild(V));

  

tv.setText(Integer.toString(ll.indexOfChild(V)));

如果你想要甚至一点点效率:

public class TestActivity extends Activity {

    private String[] categories;

    private LinearLayout ll;
    private TextView tv;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        categories = getResources().getStringArray(R.array.categories);

        tv = (TextView) findViewById(R.id.text);
        ll = (LinearLayout) findViewById(R.id.hsvLinearLayout);

        for(int i = 0; i < categories.length; i++) {
            Button btn = new Button(this);
            btn.setText(categories[i]);
            btn.setOnClickListener(buttonClick);
            ll.addView(btn);
            int idx = ll.indexOfChild(btn);
            btn.setTag(Integer.toString(idx));
        }
    }

    OnClickListener buttonClick = new OnClickListener() {
        public void onClick(View v) {
            String idxStr = (String)v.getTag();
            tv.setText(idxStr);
        }
    };

}

答案 1 :(得分:2)

在启动findViewById

之前,您必须设置SetContent
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.text);