在循环中添加带有Java LayoutInflater的视图不会';t设置宽度或仅添加循环中的第一项
我有一个从服务器加载数据的功能,比如搜索,然后将这些数据添加到主菜单中。 为了实现这一点,我在JSON结果上使用for循环来添加项 此循环工作正常,它读取数据并通过精细循环: Java循环:在循环中添加带有Java LayoutInflater的视图不会';t设置宽度或仅添加循环中的第一项,java,android,android-layout,android-linearlayout,layout-inflater,Java,Android,Android Layout,Android Linearlayout,Layout Inflater,我有一个从服务器加载数据的功能,比如搜索,然后将这些数据添加到主菜单中。 为了实现这一点,我在JSON结果上使用for循环来添加项 此循环工作正常,它读取数据并通过精细循环: Java循环: JSONArray teams = result.getJSONArray("teams"); LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); Lin
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);
}
JSONArray teams=result.getJSONArray(“teams”);
LayoutInflater充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u服务);
LinearLayout父项=(LinearLayout)主菜单.findViewById(R.id.team\u列表\u视图);
//Log.d(“TEAMS”,TEAMS.toString());
对于(int x=0;x
现在,它可以很好地循环,添加图像和文本,并附加到父布局中。但不是堆叠新布局,而是将它们并排放置,如下图所示:
在谷歌搜索之后,我尝试添加参数来设置填充父项的宽度,但结果只添加了第一项。然而,它确实添加了我想要的
我已经坚持了很长一段时间,如果有人能帮助我,我将不胜感激
我正在使用的模板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),那就更好了。 但是,您描述的行为有几个罪魁祸首:
View custom=充气机。充气(R.layout.teams\u菜单\u模板,父项,false)代码>
LinearLayout.LayoutParams params=新的LinearLayout.LayoutParams(宽度、高度)代码>
一旦创建了参数,就不会设置它们。但我认为,一旦正确地进行充气,这将是多余的。这是我将其设置为水平的父布局,将其更改为垂直,并立即工作