【问题标题】:Adding content to a ScrollView dynamically动态向 ScrollView 添加内容
【发布时间】:2014-11-18 00:32:51
【问题描述】:

我的活动中有以下布局

<HorizontalScrollView
        android:id="@+id/horizontalScrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/topHeading"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal|center_vertical">

        <LinearLayout
            android:id="@+id/scrollViewlinearlayout"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="horizontal">
            <!--
             Content will be dynamically added to this portion.
            -->
        </LinearLayout>

    </HorizontalScrollView>

在我的活动代码中,我膨胀了一个视图并添加到上面的布局中

    LinearLayout scrollViewLinearLayout=(LinearLayout)root.findViewById(R.id.scrollViewlinearlayout);
    for(int i=0;i<10;i++){
            View view =inflater.inflate(R.layout.lesson_row,null,false);
            Button b1=(Button) view.findViewById(R.id.button);
            b1.setOnClickListener(this);
            scrollViewLinearLayout.addView(view);
    }

现在这工作正常。我的滚动视图布局中有 10 个布局。现在R.layout.lesson_row 包含我想点击的按钮。我可以轻松地向按钮添加点击监听器,但是点击了 10 个布局中的哪个按钮?

【问题讨论】:

标签: android


【解决方案1】:

将 ID 或标签与您正在创建的 Button 实例相关联,以便将它们区分开来。

或者,将Button 实例放在HashMap&lt;Button, ThingTiedToButtonGoesHere&gt; 或其他Java 数据结构中,这样您就可以在运行时进行查找。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-13
    • 2012-09-05
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多