【发布时间】:2017-07-08 16:59:53
【问题描述】:
我是 Android 开发的新手,如果其他人觉得答案很明显,敬请见谅...
我想为我的组织创建一个任务跟踪器应用程序,其中的任务集通常大致相同。这是一个样机: Project Progress Mockup
模型显示通用任务名称,但真实模型将具有不同的任务名称并且更多。
我需要一些帮助来整理布局设计。从概念上讲,这很简单,但在 Android 中的实现却不是那么简单......
我想我应该创建一个包含 3 个视图的“任务项”类:任务名称的文本视图、进度条和百分比的另一个文本视图。然后我想创建一个包含这些任务项的数组。
在 XML 中设置所有这些并不是那么糟糕,但我需要它是动态的,因为它不会总是相同的列表。另外,使用 XML 有点暴力,因为我需要为这个活动中的每个项目设置唯一的代码,而不是动态地循环遍历列表。
看来我需要一个 ScrollView。我从另一个帖子中看到了以下声明:
ScrollView 是 FrameLayout,这意味着您应该在其中放置一个包含要滚动的全部内容的子视图;这个孩子本身可能是一个具有复杂对象层次结构的布局管理器。经常使用的子元素是垂直方向的 LinearLayout,呈现用户可以滚动浏览的顶级项目的垂直数组。
所以我知道我需要将我的任务项列表添加到嵌入在 ScrollView 中的 LinearLayout 之类的东西中。 我可以简单地在 XML 中设置,对吗?
然后,对于每个任务项,我需要: - 创建 2 个文本视图 - 将文本分别设置为任务名称和百分比 - 设置字体、字号、文字颜色等。 - 设置 TextView 位置 - 创建进度条 - 设置进度条位置和百分比
这一切都不是微不足道的。所以我想知道为单个任务项设置 XML 布局然后以某种方式 load() XML 布局是否更容易。希望从那里我可以简单地设置文本和进度条百分比。 这行得通,还是我做错了?另外,我将如何为此设置 XML?我是向我的项目添加一个活动,还是只是添加另一个 XML 文件?
最后,我需要任务名称是可点击的,这样我就可以捕获点击并打开一个新的活动,显示任务的子任务并允许用户更改状态。因此,任务名称 TextView 需要有一个 ID。 我应该将任务名称设为 Button 而不是 TextViews 吗?如何动态设置 ID? 我看到 Views 有一个 setId(int id) 方法。但是我怎么知道使用什么整数呢? Android 参考 View.html 声明:
视图 ID 在整个树中不必是唯一的,但这很好 实践以确保它们至少在部分中是唯一的 您正在搜索的树。
如何确保这些 ID 是唯一的?
在 XML 文件中,ID 不是数字,而是文本,例如:
android:id="@+id/my_button"
这在代码中是这样引用的:
Button myButton = findViewById(R.id.my_button);
但是如何创建和使用唯一的数字 ID 来捕获这些任务名称上的点击事件?
感谢您的指点!
新信息和问题...
我的基本功能正常工作,但有几件事不太对劲:
- 如果您查看我更新的模型Updated Project Progress Mockup,我希望以绿色突出显示的区域滚动,而其余部分保持原位。但是,顶部的标题和底部的按钮会滚出屏幕并且不会回来。不知道我做错了什么。
我的布局 xml 如下所示:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/summary"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBG"
tools:ignore="LabelFor"
tools:context="com.myorg.myapp.ProjSummary">
<TextView
android:id="@+id/txtSumm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/summ"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textColor="@color/colorPrimaryDark"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="@+id/txtProj"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:id="@+id/txtProj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/proj"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textColor="@color/colorPrimaryDark"
app:layout_constraintTop_toBottomOf="@+id/txtSumm"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/edtProj"/>
<EditText
android:id="@+id/edtProj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:paddingTop="5dp"
android:paddingBottom="0dp"
android:paddingStart="0dp"
android:paddingEnd="0dp"
android:textColor="@color/colorPrimaryDark"
android:textSize="14sp"
app:layout_constraintTop_toBottomOf="@+id/txtSumm"
app:layout_constraintLeft_toRightOf="@+id/txtProj"
app:layout_constraintRight_toRightOf="parent"/>
<ImageView
android:id="@+id/imgLogo"
android:layout_width="120dp"
android:layout_height="48dp"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btnSend"
app:srcCompat="@mipmap/my_logo"
android:contentDescription="@string/org_logo"/>
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorSecondaryDark"
android:text="@string/send"
android:textColor="@color/colorButtonText"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/imgLogo"
app:layout_constraintRight_toRightOf="parent"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvTaskList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/txtProj"
app:layout_constraintBottom_toTopOf="@+id/imgLogo"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
-
我的最后一个任务项永远不会显示。 (即,如果我有 10 个任务项,则只显示前 9 个。)在我的活动类中,我有一个用于任务项摘要列表的 ArrayList:
private ArrayList _alSummaries;
在我的 RecyclerView.Adapter 类中,我有以下方法:
@Override
public int getItemCount() { return _alSummaries.size(); }
我还应该去哪里看看?
谢谢!
进一步更新:
- 在我的布局中...
android:layout_height="wrap_content"
应该是“0dp”,而不是 wrap_content。实际上,这似乎是 Android 中的一个错误 - 如果我说 wrap_content,那绝不会导致视图变得大于可用空间(由约束设置)。
- 问题已解决 - 不是界面问题,而是将我的数据加载到应用中的代码问题。
【问题讨论】:
标签: android xml android-layout onclicklistener