我正在实现一个片段来查看时间表。这些事件中的每一个都是从 Google Calendar API 格式解析的。为了便于使用(人们将使用这个不属于我们组织的应用程序,因此他们没有对日历的读/写访问权限),我正在尝试将谷歌日历查看器烘焙到这个应用程序中(你会请注意它的风格化方式非常相似)。
下面是一个事件结构的示例:
一个事件由两个间隔 5dp 的文本视图(标题和描述)组成,它们包含在一个垂直方向的线性布局中。RelativeLayout 是此布局中的根容器。
我的问题:
这些事件通过使用它们的margin:top
值在适当的位置间隔开。margin:left
应为 55dp 以避免与左侧的时间标记重叠。我提到这一点是因为间距是让这个实现正常工作的关键,所以我需要不惜一切代价保留它。这似乎可以使用LinearLayout.LayoutParams
,但如果我弄错了,请纠正我。
但是,在我到达那里之前,我什至无法显示一个事件。我的事件信息被正确解析,计算被正确执行(通过日志声明验证),似乎正在创建 LinearLayout。但是,当我查看时间表时,我只能看到时间标记和网格线。没有事件。
下面是我用来构建 LinearLayout 并将其附加到我的根 RelativeLayout 的代码:
private void addShow(String title, String description, int startTime, int endTime) {
/* Build the LinearLayout to function as the container for this show. Since the size of the container is to represent
the length of the show, its height must be proportional (1dp = 1 minute) to the length. Determine length by finding the difference
between the start and end times. */
int difference = endTime - startTime;
/* Define the margins of this show. All shows must not overlap the displayed times, which are 50dp in width.
Add 5 more (to the right and left) to see the schedule lines for clarity. Push the show down to align with the appropriate time marker using the top margin value set to the
difference (in minutes) between midnight and the start of the show. */
RelativeLayout.LayoutParams rlLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dpToPixels(difference));
rlLayoutParams.setMargins(dpToPixels(55), dpToPixels(startTime), dpToPixels(5), 0); // l, t, r, b
/* Build LinearLayout and apply parameters */
LinearLayout eventLL = new LinearLayout(getActivity());
eventLL.setOrientation(LinearLayout.VERTICAL);
eventLL.setPadding(dpToPixels(5), dpToPixels(5), dpToPixels(5), dpToPixels(5));
eventLL.setBackgroundResource(R.drawable.orange_rounded_event);
eventLL.setLayoutParams(rlLayoutParams);
/* Add title of event to LinearLayout */
TextView titleTV = new TextView(getActivity());
titleTV.setText(title);
eventLL.addView(titleTV);
/* Determine length of event to see if we have room to attach a description (if one was passed) */
int length = endTime - startTime;
if (length >= 60 && description != null) {
TextView descriptionTV = new TextView(getActivity());
descriptionTV.setText(description);
eventLL.addView(descriptionTV);
}
/* Add this view to the schedule UI */
RelativeLayout rl = (RelativeLayout)getActivity().findViewById(R.id.schedule_container_relativelayout);
rl.addView(eventLL);
}
以防万一它可能搞砸了,这是我在 StackOverflow 上找到的我正在使用的 dp 到 px 方法:
/**
* Converts dp values to an appropriate amount of pixels based on screen density of this device.
* @param dp value of dp to convert
* @return equivalent pixel count
*/
private int dpToPixels(int dp) {
Resources r = getActivity().getResources();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
}
虽然我在 Android 开发方面有相当多的经验,但动态布局是我几乎没有接触过的东西。我不想附加整个片段,只是因为它很大,但如果需要更多上下文,我当然可以。