0

スケジュールを表示するフラグメントを実装しています。これらの各イベントは、Google Calendar API 形式から解析されます。使いやすさのために (私たちの組織に属していない人々がこのアプリを使用するため、カレンダーへの読み取り/書き込みアクセス権はありません)、Google カレンダー ビューアーをこのアプリに焼き付けようとしています (あなたは非常によく似た方法で様式化されていることに注意してください)。

イベントの構造の例を次に示します イベント構造
。イベントは、5 dp 間隔で配置された 2 つのテキストビュー (タイトルと説明) で構成され、垂直方向の LinearLayout に含まれています。RelativeLayout は、このレイアウトのルート コンテナーです。

私の問題: これらのイベントは、margin:top値を使用して適切な場所に配置されます。margin:left左側のタイム マーカーと重ならないように、55 dp にする必要があります。これについて言及するのは、この実装が正しく機能するために間隔が重要であるためです。これは を使用して可能のよう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);
}

そして、それが何かを台無しにしている可能性がある場合に備えて、私が使用している dp to px メソッドは、StackOverflow で見つけたものです。

/**
 * 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 開発でかなりの経験を積んできましたが、動的レイアウトにはほとんど触れたことがありません。かなり大きいという理由だけで Fragment 全体を添付したくはありませんでしたが、より多くのコンテキストが必要な場合は確かに添付できます。

4

1 に答える 1

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);
于 2013-10-12T01:41:29.557 に答える