gpt4 book ai didi

java - 在循环中使用 Java LayoutInflater 添加 View 要么不设置宽度,要么只添加循环中的第一项

转载 作者:行者123 更新时间:2023-11-30 01:57:22 25 4
gpt4 key购买 nike

我有从服务器加载数据的功能,例如搜索,然后将这些添加到主菜单中。为此,我在 JSON 结果上使用 for 循环来添加项目。

这个循环工作正常,它读取数据并很好地循环:

Java 循环:

JSONArray teams = result.getJSONArray("teams");
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout parent = (LinearLayout) mainMenu.findViewById(R.id.team_list_view);
//Log.d("TEAMS",teams.toString());
for(int x = 0; x < teams.length(); x++) {
JSONObject cTeam = teams.getJSONObject(x);
String name = cTeam.getString("name");
String thumb = cTeam.getString("thumb");
String id = cTeam.getString("id");
View custom = inflater.inflate(R.layout.teams_menu_template, null);
int width = LinearLayout.LayoutParams.FILL_PARENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;

ImageButton pp = (ImageButton) custom.findViewById(R.id.tempPPbtn);
Button teamName = (Button) custom.findViewById(R.id.tempPPTxtbtn);
teamName.setText(name);

loadImage loadImage = new loadImage("imagebutton",pp);
loadImage.execute(thumb);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height);

parent.addView(custom);

}

现在这确实工作正常,它循环并添加图像和文本并附加到父布局。但不是堆叠新布局,而是将它们并排放置,如下图所示:

side by instead of stacked

谷歌搜索后,我尝试添加参数以将宽度设置为 FILL_PARENT,但结果只添加了第一项。但是它确实按照我的意愿添加它。 with big gap

我已经坚持了很长一段时间,如果有人能提供帮助,我将不胜感激。

我正在使用的模板 XML 文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="@+id/tempDropCont"
android:background="@drawable/drop_down"
android:weightSum="100"
android:baselineAligned="true">

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2">

<LinearLayout
android:orientation="vertical"
android:layout_width="70dp"
android:layout_height="wrap_content">

<ImageButton
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/tempPPbtn"
android:background="@drawable/profile"
android:layout_marginLeft="15dp" />
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/leader_board"
android:id="@+id/tempPPTxtbtn"
android:background="@android:color/transparent"
android:textColor="@color/white"
android:textSize="20sp"
android:layout_marginTop="10dp" />

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tempDrop"
android:visibility="gone">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/view_team"
android:id="@+id/tempTxtBtn1"
android:background="@android:color/transparent"
android:textColor="@color/white"
android:textSize="20sp"
android:layout_marginTop="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/edit_team"
android:id="@+id/tempTxtBtn2"
android:background="@android:color/transparent"
android:textColor="@color/white"
android:textSize="20sp"
android:layout_marginTop="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/team_settings"
android:id="@+id/tempTxtBtn3"
android:background="@android:color/transparent"
android:textColor="@color/white"
android:textSize="20sp"
android:layout_marginTop="10dp" />
</LinearLayout>

</LinearLayout>
</LinearLayout>
</LinearLayout>

起初我确实认为它是 xml,但我尝试在不同的布局上使用 include,它也像预期的那样包含文件。

注意服务器返回了两个项目。

最佳答案

最好也有父布局,或者至少是您定义父布局的方式(带有 id listview 的 LinearLayout)。但是,您描述的行为有几个罪魁祸首:

  1. 确保父布局的方向设置为垂直。此时你可以复制粘贴几个模板项目到你的布局中,看看当你在 xml 中定义它们时它们看起来是否正常

  2. 当你膨胀你的项目时,你还需要传递父项,以便子项继承布局属性:

    View custom = inflater.inflate(R.layout.teams_menu_template, parent, false);

这将创建具有父容器中定义的预期属性的项目,但尚未将其附加到父容器。

  1. 这条线没有用到:

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height);

一旦创建参数,就无需再设置它们。但我认为,一旦你正确地进行了通货膨胀,这将是多余的。

关于java - 在循环中使用 Java LayoutInflater 添加 View 要么不设置宽度,要么只添加循环中的第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32024054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com