- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有从服务器加载数据的功能,例如搜索,然后将这些添加到主菜单中。为此,我在 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);
}
现在这确实工作正常,它循环并添加图像和文本并附加到父布局。但不是堆叠新布局,而是将它们并排放置,如下图所示:
谷歌搜索后,我尝试添加参数以将宽度设置为 FILL_PARENT,但结果只添加了第一项。但是它确实按照我的意愿添加它。
我已经坚持了很长一段时间,如果有人能提供帮助,我将不胜感激。
我正在使用的模板 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)。但是,您描述的行为有几个罪魁祸首:
确保父布局的方向设置为垂直。此时你可以复制粘贴几个模板项目到你的布局中,看看当你在 xml 中定义它们时它们看起来是否正常
当你膨胀你的项目时,你还需要传递父项,以便子项继承布局属性:
View custom = inflater.inflate(R.layout.teams_menu_template, parent, false);
这将创建具有父容器中定义的预期属性的项目,但尚未将其附加到父容器。
这条线没有用到:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height);
一旦创建参数,就无需再设置它们。但我认为,一旦你正确地进行了通货膨胀,这将是多余的。
关于java - 在循环中使用 Java LayoutInflater 添加 View 要么不设置宽度,要么只添加循环中的第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32024054/
好吧,这很奇怪。当我在模拟器中运行应用程序时,我的 logcat 突然发生崩溃,之前我从未遇到过它,并且自上次在模拟器中运行应用程序以来我没有对我的应用程序进行任何更改几周前,它正在运行。 这是完整的
我使用了构造函数,但它不起作用。 LayoutInflater layoutInflater = new LayoutInflater(getContext()); Android Studio 用红
至少我认为这是问题所在。来自 API 文档: inflate(int resource, ViewGroup root) Inflate a new view hierarchy from the s
我有一个用于 listView 的自定义适配器。当我尝试膨胀某些 View 时,它会抛出异常。这是我的 getView() 代码: public View getView(int position,
我是 Android 开发的新手,我对 Android 开发社区中流传的“Inflate a View”这个词很好奇。 我知道它与调用 LayoutInflater 类的 Inflate 方法有关,但
这是我的文件代码,旨在使用布局动态添加 View 我指的是 http://maxmakedesign.co.uk/how-to/80/android-add-views-in-a-loop packa
我想在 OnCreateView 中设置 wordtoSpan 字符串。 我的代码: package com.tachles; import android.os.Bundle; import and
我有以下表示 ImageButton 的 xml 对象 我尝试使用以下代码将其膨胀并添加到 TableRow 中: LayoutInflater inflater= this.getLayout
事情是这样的,我有一个 RecycleView 适配器,它可以在 onCreateViewHolder 上扩展布局。当我尝试执行以下操作以发生这种情况时: 应用创建没有任何问题 进入多任务窗口 回到应
我只是偶尔在一台设备上遇到这个错误: 04-09 16:05:22.016: E/AndroidRuntime(31843): FATAL EXCEPTION: main 04-09 16
这是我的类 DownloadableProjectsFromWebAdapter 的 getView 方法: @Override public View getView(int position, V
我的问题是创建 LayoutInflater 实例的最佳方法是什么?有什么区别吗 LayoutInflater inflater = LayoutInflater.from(context); 和 L
我正在考虑编写一个自定义适配器来填充一个 ListView ,每行 3 个 TextView 。我找到了相当多的示例代码来执行此操作,但看起来最好的代码位于:http://www.anddev.org
我在 LayoutInflater 代码中看到实际 View 类是 instantiated via reflection (LayoutInflater - createView() 方法),但是,
我有一个 Fragment,它在 onCreateView 方法中有很多代码,所以我认为将这些代码放在外部线程中是个好主意。现在的问题是我必须在此线程中加载布局,但是如果我在 Activity 的 o
我们目前正在大学里使用 ListView 。我的讲师给了我们一个简单的应用程序,它在列表中显示邮件消息,当用户选择一个时,它会在新 Activity 中显示消息的内容。我几乎了解所有发生的事情,但我想
我对这段代码感到困惑 在这里,他们使用了抽象类并继承了它,最重要的是,他们在不创建对象的情况下调用了方法 附注完整代码:- https://github.com/udacity/ud839_Custo
我正在开发一个具有抽屉导航并且需要用户登录的应用程序。 到目前为止,我已经完成了整个用户登录/注册工作,并且我已经构建了抽屉导航。 我现在想做的是让抽屉导航上的 TextView 显示用户的全名和他/
我正在寻找一种方法来“扩充”Android XML 布局,这在编译时是未知的。我已经搜索了几个小时,总能找到答案,这是不可能的,因为 LayoutInflater 不能使用简单的 XML 文件。好的,
我试图在回收站 View 适配器中创建一个弹出窗口,以便在用户单击回收站 View 的某个项目时显示一个小菜单。这是弹出功能: public void Popup(final int id) {
我是一名优秀的程序员,十分优秀!