如何在其他人的右侧添加ImageView / textView ImageView / textView采用相同的布局(How to dinamically add ImageView/textVie

编程入门 行业动态 更新时间:2024-10-24 02:26:39
如何在其他人的右侧添加ImageView / textView ImageView / textView采用相同的布局(How to dinamically add ImageView/textView at the right of others ImageView/textView in a same layout)

所以在我的代码中,我有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与objet的数量,绿色的对象的imageView ... https://gyazo.com/31b16982ce30b66391bafdd2cd4d86fc

我发现了一些与LayoutInflater有关的东西,但我还没有成功使用这些...非常感谢你能帮到我。

PS:对不起,我认为有很多,但在这样的长篇文章中,我的学校英语不是很有效^^再次感谢:)

So in my code I have 6 arrays (the 5 last could be empty) of objects. Each objects has a name and the array could have many of each object (every array are sorted so every item are grouped).

First, I have this xml file:

<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>

It look like this: https://gyazo.com/17a1276dfff5521d540fb6dc953df424

What I want to do is, for each object in each array (one array: one linear layout), to add a textView with the number of Item and an imageView which represent the object.

Next, you will find how I sort everything, that's not really important I think, but if you want to understand everything you'll have to give a look :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);

Now we go for my problem, this portion of code is just next to the one I put previously, here is how I pick up the number of the same object in each array

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++; }

And finally here is how I don't really know how to do:

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); } }

With this code I have an insane black screen when I'm going on this activity. Here is what I want to do with in red the textView with the number of objet and in green the imageView of the object... https://gyazo.com/31b16982ce30b66391bafdd2cd4d86fc

I've found some stuff to do with LayoutInflater but I haven't been successfull with these... Thanks a lot if you could help me.

PS: sorry for the mistakes, I think there are a lot, but in long post like this one, my school english isn't very effective ^^ Thanks again :)

最满意答案

如我错了请纠正我。 但根据我的理解,你只需要完成这里显示的内容https://gyazo.com/31b16982ce30b66391bafdd2cd4d86fc ?

根据您的代码,您只需在添加TextView和图像视图之前在每个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的布局错误,请随意调整它。 但基本上,这就是主意。

Correct me if I'm wrong. But from what I understood you just need to accomplish what is being shown here https://gyazo.com/31b16982ce30b66391bafdd2cd4d86fc?

Base on your code you just have to add an additional LinearLayout having an horizontal orientation on each LinearLayout before adding the TextView and Image View.

Here's an example using your code:

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); }

Feel free to adjust it if the Layout that I'm adding the innerLayout in is wrong. But basically, that's the idea.

更多推荐

本文发布于:2023-08-07 07:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462631.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布局   其他人   如何在   ImageView   textView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!