将LinearLayout添加到RelativeLayout时出现问题

时间:2013-10-11 22:12:08

标签: android android-layout google-calendar-api

我正在实施一个片段来查看时间表。每个事件都是从Google Calendar API格式解析的。为了便于使用(人们将使用这个不属于我们组织的应用程序,因此他们将无法读取/写入日历),我正在尝试将Google日历查看器烘焙到此应用程序中(您将注意它以非常类似的方式风格化。)

以下是事件结构的示例: event structure
事件由两个相隔5dp的文本视图(标题和描述)组成,它们包含在垂直方向的LinearLayout中。 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 to 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开发中拥有相当多的经验,但动态布局是我几乎没有触及过的。我不想附加整个片段,因为它非常大,但如果需要更多上下文,我当然可以。

1 个答案:

答案 0 :(得分:1)

对于可能在搜索引擎中遇到此问题的任何人,我找到了解决方法。

出于某种原因,当我使用RelativeLayout.LayoutParams成员的公共方法直接定义边距时,它起了作用。

RelativeLayout.LayoutParams rrLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, dpToPixels(difference));
rrLayoutParams.leftMargin = dpToPixels(55);
rrLayoutParams.topMargin = dpToPixels(startTime);
rrLayoutParams.rightMargin = dpToPixels(5);

我必须将参数传递给addView()

rl.addView(eventLL, rrLayoutParams);
相关问题