【问题标题】:How to dinamically add ImageView/textView at the right of others ImageView/textView in a same layout如何在同一布局中的其他ImageView/textView右侧动态添加ImageView/textView
【发布时间】:2016-04-09 16:45:16
【问题描述】:

所以在我的代码中,我有 6 个对象数组(最后 5 个可能为空)。每个对象都有一个名称,并且数组可以包含多个对象(每个数组都经过排序,因此每个项目都被分组)。

首先,我有这个 xml 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fond4"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20dp"
    android:background="@color/white"
    android:gravity="center"
    android:text="@string/commentOpti"
    android:textSize="20sp" />

<Button
    android:id="@+id/choisirTroupe"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20dp"
    android:onClick="soumission"
    android:text="@string/lancer" />

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:context="${relativePackage}.${activityClass}" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="20dp"
            android:background="@color/white"
            android:gravity="center"
            android:text="@string/casernes"
            android:textSize="20sp" />

        <!-- Caserne 1 -->

        <LinearLayout
            android:id="@+id/caserne1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@color/white"
            android:orientation="horizontal" >

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginLeft="10dp"
                android:background="@drawable/caserne" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="@string/deuxPoints"
                android:textSize="25sp" />
        </LinearLayout>

        //Then 5 other like this 1st linearLayou
        //The ids are incremented each time like caserne2 for the 2nd etc...


</ScrollView>

看起来像这样:https://gyazo.com/17a1276dfff5521d540fb6dc953df424

我想要做的是,对于每个数组中的每个对象(一个数组:一个线性布局),添加一个带有 Item 数量的 textView 和一个代表该对象的 imageView。

接下来,你会发现我如何对所有内容进行排序,我认为这并不重要,但如果你想了解所有内容,你必须看看:p

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_optimisation);

    LinearLayout caserne1 = (LinearLayout) findViewById(R.id.caserne1);
    //...Doing this for the 5 others...

    Intent intent = getIntent();


    //Instanciation of my object
    OptiClashOfClans o = new OptiClashOfClans();

    //In fact, the number of linearLayout i'll have to work with
    o.setNbCasernes(this.nbCaserne);

    if (this.nbBarbares > 0)
        o.ajouterFormation(1, this.nbBarbares);
    //...Adding each object in on array...      
    o.setAFormer(o.triTroupe());

    //And finally here i'll put each object in the good array
    o.orgaCaserne(o);

现在我们来解决我的问题,这部分代码就在我之前放置的代码旁边,这是我如何在每个数组中获取相同对象的编号

for (int cptCaserne = 0; cptCaserne < this.nbCaserne; cptCaserne++) {


        // Here I pick up the array of object I'll work with
        Caserne casTmp = o.getCaserneNum(cptCaserne);


        // Here I pick every object which are in the previous array
        ArrayList<Troupe> enFormTmp = new ArrayList<Troupe>();
        enFormTmp = casTmp.getEnFormation();

        // To count where I am in the array of object
        int cptAFormer = 0;         

        //To know if the next object is different from the one I'm working with
        Troupe troupTmp = enFormTmp.get(cptAFormer);

        int cptTroupTmp = 0;

        //For the object t in the array of object
        for (Troupe t : enFormTmp) {
            cptTroupTmp = 0;

            //While the object is the same from the one we're working with
            while (!troupTmp.equals(t)) {
                //Incrementing the previous counter
                cptTroupTmp++;
                cptAFormer++;
            }

最后是我真的不知道该怎么做:

            ImageView iView = new ImageView(Optimisation.this);

            //To know which background i'll add to the image view
            //I'll do this for each different object
            if (t.getNom().equals("Barbare"))
                iView.setImageResource(R.drawable.barbare);
            //...

            //The text view with the number of object I found
            TextView tView = new TextView(Optimisation.this);
            tView.setText("" + cptTroupTmp);


            //And now it's here where I want to add the different views
            switch (cptCaserne) {
            case 0:
                caserne1.addView(tView);
                caserne1.addView(iView);
            case 1:
                caserne2.addView(tView);
                caserne1.addView(iView);
            case 2:
                caserne3.addView(tView);
                caserne1.addView(iView);
            case 3:
                caserne4.addView(tView);
                caserne1.addView(iView);

            }
            troupTmp = enFormTmp.get(cptAFormer);
        }

    }

使用此代码时,我在进行此活动时出现了疯狂的黑屏。 这是我想要用红色的 textView 和对象的数量和绿色的对象的 imageView 做的事情...... https://gyazo.com/31b16982ce30b66391bafdd2cd4d86fc

我找到了一些与 LayoutInflater 相关的东西,但我没有成功...如果你能帮助我,非常感谢。

PS:对不起,我觉得有很多错误,但是在这样的长帖子中,我的学校英语不是很有效^^ 再次感谢:)

【问题讨论】:

    标签: android arrays android-layout textview imageview


    【解决方案1】:

    如果我错了,请纠正我。但据我了解,您只需要完成此处显示的内容https://gyazo.com/31b16982ce30b66391bafdd2cd4d86fc

    根据您的代码,您只需在添加 TextView 和 Image View 之前在每个 LinearLayout 上添加一个具有水平方向的附加 LinearLayout。

    这是使用您的代码的示例:

    LinearLayout innerLayout = new LinearLayout(Optimisation.this)
    innerLayout.setOrientation(LinearLayout.HORIZONTAL);
    ImageView iView = new ImageView(Optimisation.this);
    
            //To know which background i'll add to the image view
            //I'll do this for each different object
            if (t.getNom().equals("Barbare"))
                iView.setImageResource(R.drawable.barbare);
            //...
    
            //The text view with the number of object I found
            TextView tView = new TextView(Optimisation.this);
            tView.setText("" + cptTroupTmp);
    
    
            innerLayout.addView(tView);
            innerLayout.addView(iView);
    
            //And now it's here where I want to add the different views
            switch (cptCaserne) {
            case 0:
                caserne1.addView(innerLayout);
            case 1:
                caserne2.addView(innerLayout);
            case 2:
                caserne3.addView(innerLayout);
            case 3:
                caserne4.addView(innerLayout);
    
            }
    

    如果我添加 innerLayout 的布局错误,请随时调整它。但基本上,就是这样。

    【讨论】:

    • with "Optimisation.this" eclypse 说我优化无法解析为一种类型,但它仍然无法正常工作,但你给了我一个想法,我会尝试非常感谢 :)跨度>
    • new LinearLayout(Optimisiation.this) 将其替换为:new LinearLayout(this)
    • 当我按下按钮进入这个活动时,我仍然有一个黑屏,然后它说应用程序没有响应......这就是你的答案,它可能来自错误用我的代码? (不是对所有内容进行排序的人,我已经进行了很多测试并且它正在工作,但是我粘贴在下面的最后三个中的一个?)
    • 你的申请最终通过了吗?它崩溃了吗?你的日志是怎么说的?
    • 我的日志什么也没说,只是我的应用程序在我的平板电脑弹出时崩溃了:“没有响应,你想等待还是关闭它”或类似的东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多