没有动态地设置公交车座椅的布局

时间:2016-06-18 06:18:20

标签: android bus

我参与过公交车票预订申请。我为座位布局编写代码,但座位显示在一列中。我不知道为什么只有一列显示。但我想看起来像下面的图像 i want this output i get this type of output

我用的代码

void prepareSeatChart() {
        try {
            RelativeLayout[] r = new RelativeLayout[]{(RelativeLayout) findViewById(R.id.lowerDeckRelativeLayout), (RelativeLayout) findViewById(R.id.upperDeckRelativeLayout)};
            int i = this._cellWidth;
            if (this._cellWidth < 1) {
                int totalHorizontalMargin = getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin) * 2;
                this._cellWidth = (getWindowManager().getDefaultDisplay().getWidth() - totalHorizontalMargin) / this._cols;
                if (this._cols < 6) {
                    this._cellWidth = (getWindowManager().getDefaultDisplay().getWidth() - totalHorizontalMargin) / 6;
                }
                this._cellHeight = (this._cellWidth * 82) / Constants.AppCompatTheme_ratingBarStyle;
            }
            if (this._hasCompartments[0]) {
                findViewById(R.id.lowerDeckLinearLayout).setVisibility(View.VISIBLE);
                findViewById(R.id.upperDeckLinearLayout).setVisibility(View.GONE);
                findViewById(R.id.lowerDeckRelativeLayout).setVisibility(View.GONE);
                findViewById(R.id.upperDeckRelativeLayout).setVisibility(View.GONE);
            } else {
                findViewById(R.id.lowerDeckLinearLayout).setVisibility(View.GONE);
                findViewById(R.id.upperDeckLinearLayout).setVisibility(View.GONE);
                findViewById(R.id.lowerDeckRelativeLayout).setVisibility(View.VISIBLE);
                findViewById(R.id.upperDeckRelativeLayout).setVisibility(View.GONE);
            }
            if (this._seatWidth == 0) {
                this._seatWidth = ((r[0].getWidth() - 20 - this._margin * 2 * this._cols) / this._cols);
                if (this._cols < 6) {
                    this._seatWidth = ((r[0].getWidth() - 20 - this._margin * 2 * 6) / 6);
                }
            }
            int w1 = this._seatWidth;
            View emptyView = new View(this);
            emptyView.setId((((this._rows * 10) + Constants.AUTH_API_INVALID_CREDENTIALS) + this._cols) + 2);
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.drawable.steering_icon);
            imageView.setScaleType(ScaleType.CENTER);
            imageView.setId((((this._rows * 10) + Constants.AUTH_API_INVALID_CREDENTIALS) + this._cols) + 1);
            int width = (this._cellWidth * 64) / Constants.AppCompatTheme_ratingBarStyle;
            int height = (this._cellWidth * 72) / Constants.AppCompatTheme_ratingBarStyle;
            int mLeft = (this._cellWidth * 21) / Constants.AppCompatTheme_ratingBarStyle;
            int mRight = mLeft;
            int mTop = (int) (((double) ((this._cellWidth * 5) / Constants.AppCompatTheme_ratingBarStyle)) * 1.5d);
            int mBottom = mTop;

            if (this._cellWidth != (width + mLeft) + mRight) {
                width = (this._cellWidth + width) - ((width + mLeft) + mRight);
            }
            if (this._cellHeight != (height + mTop) + mBottom) {
                height = (this._cellHeight + height) - ((height + mTop) + mBottom);
            }
            emptyView.setLayoutParams(new RelativeLayout.LayoutParams(this._cellWidth * (this._cols - 1), height));
            r[0].addView(emptyView);
            RelativeLayout.LayoutParams imageViewParams = new RelativeLayout.LayoutParams(width, height);
            imageViewParams.setMargins(mLeft, mTop, 0, mBottom);
            imageViewParams.addRule(10, -1);
            imageViewParams.addRule(1, emptyView.getId());
            imageView.setLayoutParams(imageViewParams);
            r[0].addView(imageView);
            for (int d = 0; d < 2; d++) {
                if (!this._hasCompartments[d]) {
                    int i4 = 0;
                    while (true) {
                        if (i4 >= this._rows) {
                            break;
                        }
                        int j = 0;
                        while (true) {
                            if (j >= this._cols) {
                                break;
                            }
                            if (this._seat[d][i4][j] == null) {
                                PrivateBusSeatGeSe s = new PrivateBusSeatGeSe();
                                s.setDeck(d);
                                s.setSeatColumns(j);
                                s.setSeatRows(i4);
                                s.setID((((s.getDeck() * BaseActivity.RESULT_CODE_ERROR) + (s.getSeatRows() * 10)) + s.getSeatColumns()) + 1);
                                this._seat[d][i4][j] = s;
                            }
                            if ((((d * BaseActivity.RESULT_CODE_ERROR) + (i4 * 10)) + j) + 1 == this._seat[d][i4][j].getID()) {
                                Button button = new Button(this);
                                button.setTextColor(getResources().getColor(R.color.btn_text_shadow));
                                if (this._seat[d][i4][j].getSelected()) {
                                    button.setText(this._seat[d][i4][j].getSeatNo());
                                }
                                button.setId(this._seat[d][i4][j].getID());
                                button.setBackgroundResource(getSeatBackGroundResource(d, i4, j));
                                if (this._seat[d][i4][j].getAvailable()) {
                                    button.setTag(this._seat[d][i4][j].getSeatNo());
                                    button.setOnClickListener(new View.OnClickListener() {
                                        public void onClick(View paramAnonymousView) {
                                            try {
                                                PrivateBusSeatLayout.this.onSeatButtonClick(paramAnonymousView);
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    });
                                }
                                int w;
                                int h;
                                int mL;
                                int mR;
                                int mT;
                                int mB;
                                ViewGroup.MarginLayoutParams layoutParams;
                                if (this._seat[d][i4][j].getSeatWidth() == 2) {
                                    w = (this._cellWidth * 154) / Constants.AppCompatTheme_ratingBarStyle;
                                    h = (this._cellWidth * 54) / Constants.AppCompatTheme_ratingBarStyle;
                                    mL = (this._cellWidth * 29) / Constants.AppCompatTheme_ratingBarStyle;
                                    mR = mL;
                                    mT = (this._cellWidth * 14) / Constants.AppCompatTheme_ratingBarStyle;
                                    mB = mT;
                                    if (this._cellWidth * 2 != (w + mL) + mR) {
                                        w = ((this._cellWidth * 2) + w) - ((w + mL) + mR);
                                    }
                                    i = this._cellHeight;
                                    if (this._cellHeight != (h + mT) + mB) {
                                        h = (this._cellHeight + h) - ((h + mT) + mB);
                                    }
                                    layoutParams = new RelativeLayout.LayoutParams(w, h);
                                    layoutParams.setMargins(mL, mT, mR, mB);
                                } else {
                                    if (this._seat[d][i4][j].getSeatHeight() == 2) {
                                        w = (this._cellWidth * 54) / Constants.AppCompatTheme_ratingBarStyle;
                                        h = (this._cellWidth * 154) / Constants.AppCompatTheme_ratingBarStyle;
                                        mL = (this._cellWidth * 26) / Constants.AppCompatTheme_ratingBarStyle;
                                        mR = mL;
                                        mT = (this._cellWidth * 5) / Constants.AppCompatTheme_ratingBarStyle;
                                        mB = mT;
                                        i = this._cellWidth;
                                        if (this._cellWidth != (w + mL) + mR) {
                                            w = (this._cellWidth + w) - ((w + mL) + mR);
                                        }
                                        if (this._cellHeight * 2 != (h + mT) + mB) {
                                            h = ((this._cellHeight * 2) + h) - ((h + mT) + mB);
                                        }
                                        layoutParams = new RelativeLayout.LayoutParams(w, h);
                                        layoutParams.setMargins(mL, mT, mR, mB);
                                    } else {
                                        w = (this._cellWidth * 64) / Constants.AppCompatTheme_ratingBarStyle;
                                        h = (this._cellWidth * 72) / Constants.AppCompatTheme_ratingBarStyle;
                                        mL = (this._cellWidth * 21) / Constants.AppCompatTheme_ratingBarStyle;
                                        mR = mL;
                                        mT = (this._cellWidth * 5) / Constants.AppCompatTheme_ratingBarStyle;
                                        mB = mT;
                                        i = this._cellWidth;
                                        if (this._cellWidth != (w + mL) + mR) {
                                            w = (this._cellWidth + w) - ((w + mL) + mR);
                                        }
                                        i = this._cellHeight;
                                        if (this._cellHeight != (h + mT) + mB) {
                                            h = (this._cellHeight + h) - ((h + mT) + mB);
                                        }
                                        layoutParams = new RelativeLayout.LayoutParams(w, h);
                                        layoutParams.setMargins(mL, mT, mR, mB);
                                    }
                                }
                                if (i4 == 0) {
                                    if (d == 0) {
                                        ((RelativeLayout.LayoutParams) layoutParams).addRule(3, imageView.getId());
                                    } else {
                                        ((RelativeLayout.LayoutParams) layoutParams).addRule(10, -1);
                                    }
                                }
                                if (j == 0) {
                                    ((RelativeLayout.LayoutParams) layoutParams).addRule(9, -1);
                                }
                                if (i4 > 0) {
                                    ((RelativeLayout.LayoutParams) layoutParams).addRule(3, this._seat[d][i4 - 1][j].getID());
                                }
                                if (j > 0) {
                                    ((RelativeLayout.LayoutParams) layoutParams).addRule(1, this._seat[d][i4][j - 1].getID());
                                }
                                button.setPadding(0, 0, 0, 0);
                                ((Button) button).setTextSize(10.0f);
                                button.setLayoutParams(layoutParams);
                                r[d].addView(button);
                            }
                            j++;
                        }
                        i4++;
                    }
                } else {
                    prepareSeatChartWithCompartments(d);
                }
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
void prepareSeatChartWithCompartments(int d) {
        LinearLayout[] l = new LinearLayout[]{(LinearLayout) findViewById(R.id.lowerDeckLinearLayout), (LinearLayout) findViewById(R.id.upperDeckLinearLayout)};
        int w = ((getWindowManager().getDefaultDisplay().getWidth() - (this._margin * 6)) - 10) / 6;
        int seatCount = 0;
        Log.d("prpeare compartments","" + this._seatList.size());
        for (int i = 0; i < this._seatList.size(); i++) {
            PrivateBusSeatGeSe s = this._seatList.get(i);
            if (s.getDeck() == d) {
                View v;
                LinearLayout.LayoutParams params;
                if (seatCount % 2 == 0 && seatCount > 0) {
                    v = new View(this);
                    v.setBackgroundColor(getResources().getColor(R.color.gray));
                    int width = s.getSeatWidth() * w;
                    params = new LinearLayout.LayoutParams(-1, 1);
                    params.setMargins(w, 4, 0, 4);
                    v.setPadding(0, 0, 0, 0);
                    v.setLayoutParams(params);
                    l[d].addView(v);
                }
                if (seatCount % 2 == 1) {
                    v = new View(this);
                    params = new LinearLayout.LayoutParams(s.getSeatWidth() * w, (s.getSeatHeight() * w) / 2);
                    params.setMargins(w, 0, 0, 0);
                    v.setPadding(0, 0, 0, 0);
                    v.setLayoutParams(params);
                    l[d].addView(v);
                }
                Button v2 = new Button(this);
                v2.setTextColor(getResources().getColor(R.color.btn_text_shadow));
                if (s.getSelected()) {
                    v2.setText(s.getSeatNo());
                }
                v2.setId(s.getID());
                v2.setBackgroundResource(getSeatBackGroundResourceWithSeat(s));
                params = new LinearLayout.LayoutParams(s.getSeatWidth() * w, s.getSeatHeight() * w);
                if (s.getSeatWidth() == 2) {
                    params.setMargins((this._margin * 2) + w, this._margin, this._margin * 2, this._margin);
                } else if (s.getSeatHeight() == 2) {
                    params.setMargins(this._margin + w, this._margin * 2, this._margin, this._margin * 2);
                } else {
                    params.setMargins(this._margin + w, this._margin, this._margin, this._margin);
                }
                v2.setPadding(0, 0, 0, 0);
                v2.setTextSize(10.0f);
                v2.setLayoutParams(params);
                if (s.getAvailable()) {
                    v2.setTag(s.getSeatNo());
                    v2.setOnClickListener(new View.OnClickListener()
                    {
                        public void onClick(View paramAnonymousView)
                        {
                            try {
                                PrivateBusSeatLayout.this.onSeatButtonClick(paramAnonymousView);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
                l[d].addView(v2);
                seatCount++;
            }
        }
    }

int getSeatBackGroundResourceWithSeat(PrivateBusSeatGeSe s) {
    String seatTypeStr = "";
    String seatDirectionStr = "";
    String seatGenderStr = "";
    String seatStatusStr = "";
    if(!s.getEmpty())
    {
        if (s.getSleeper()) {
            seatTypeStr = Constants.GA_PRODUCT_VARIANT_SLEEPER;
            if (s.getSeatWidth() == 2) {
                seatDirectionStr = "_h";
            } else if (s.getSeatHeight() == 2) {
                seatDirectionStr = "_v";
            }
        } else if (s.getSeatNo().trim().length() > 0) {
            seatTypeStr = Constants.GA_PRODUCT_VARIANT_SEATER;
        }
        if (s.getSelected()) {
            seatStatusStr = "_selected";
        } else {
            if (s.getAvailable()) {
                seatStatusStr = "_available";
            } else {
                seatStatusStr = "_booked";
            }
            if (s.getLadies()) {
                seatGenderStr = "_female";
            } else {
                seatGenderStr = "_male";
            }
        }
        if (seatTypeStr.trim().length() > 0) {
            return this._seatLegendMap.get("ico_" + seatTypeStr + seatStatusStr + seatGenderStr + seatDirectionStr);
        }
        if (this._emptyColomn == -1 && s.getSeatColumns() > 0 && s.getSeatColumns() < this._cols) {
            this._emptyColomn = s.getSeatColumns();
        }
    }
    return 0;
}

0 个答案:

没有答案