Android将容器添加到horizo​​nalscollview

时间:2017-01-11 02:19:31

标签: java android xml layout

我必须承认我的java生锈了,这是我的第一个原生Android应用程序。几年前我使用Titanium工作室构建了相同的应用程序。

我的main_activity.xml中有一个Horizo​​ntalScroll视图,我希望每次用户按下按钮时,都会将LinearLayout容器添加到数据中。所以我的滚动视图将从零开始,每个按钮按下将添加另一个条目。

我尝试使用单独的布局来添加容器,使用充气机,但后来看到我只是在主活动布局中定义它的迹象。也许这是正确的路线这是我的布局的相关部分

<HorfizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ResultView">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:onClick="onShowResult"
    android:clickable="true"
    android:visibility="gone"
    android:id="@+id/ResultContainer">

    <!--successes-->
    <LinearLayout
        android:orientation="horizontal"
        android:weightSum="2"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:text="@string/success_label"
            android:gravity="end"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/Success_Label"
            android:layout_weight="1"
            android:clickable="false" />
        <TextView
            android:text=""
            android:gravity="end"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/Successes"
            android:layout_weight="1"
            android:clickable="false" />
    </LinearLayout>
    <!--tenss-->
    <LinearLayout
        android:orientation="horizontal"
        android:weightSum="2"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:text="@string/tens_result_label"
            android:gravity="end"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/Tens_Result_Label"
            android:layout_weight="1"
            android:clickable="false" />
        <TextView
            android:text=""
            android:gravity="end"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/Tens_Result"
            android:layout_weight="1"
            android:clickable="false" />
    </LinearLayout>
    <!--ones-->
    <LinearLayout
        android:orientation="horizontal"
        android:weightSum="2"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:text="@string/ones_result_label"
            android:gravity="end"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/Ones_Result_Label"
            android:layout_weight="1"
            android:clickable="false" />
        <TextView
            android:text=""
            android:gravity="end"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/Ones_Result"
            android:layout_weight="1"
            android:clickable="false" />
    </LinearLayout>
</LinearLayout>

以下是我开始使用的代码,用于在我的OnClick按钮中实现它。 (条目本身最终将是可点击的)这是onClick相关的唯一部分,因为其余部分只是在进行计算,而不是创建要添加的视图。

    //create and insert results
    //start by getting the result view we plan to insert to
    HorizontalScrollView resultview = (HorizontalScrollView)findViewById(R.id.ResultView);
    //pull up the container to insert
    LinearLayout resultcontainer = (LinearLayout)findViewById(R.id.ResultContainer);
    //get and set the results

    //add the container to the resultsview
    resultview.addView(resultcontainer);
    resultcontainer.setVisibility(View.VISIBLE);

这导致我的应用程序崩溃,调试似乎表明它有OnClick,但我不清楚。具体来说,它看起来像倒数第二行中的addview导致崩溃。

我相当肯定我不理解关于如何做到这一点的非常基本和基本的东西,但就像我说的那样,有点潜入并随时学习。

我还需要在我添加的容器中编辑TextView的文本,但一次只能编辑一件事。

任何有关正确方向的帮助或指示都将受到赞赏。

所以,我的问题,因为显然有一些混乱。将LinearLayouts添加到HorrizontalScrollView的最佳方法是什么,这样我可以在那些LinearLayouts中访问和修改TextViews? 理想情况下,我想创建最初通过XML布局添加的对象,而不是完全以编程方式构建它们。创建和修改后,我会将它们添加到我的HorrizontalScroll View中。

0 个答案:

没有答案