1

目前我从 URL 获取以下 XML 数据格式-

<Seat>
<ColumnNo>0</ColumnNo>
<Deck>2</Deck>
<Height>2</Height>
<IsAvailable>1</IsAvailable>
<RowNo>0</RowNo>
<SeatLabel>E</SeatLabel>
<Width>1</Width>
</Seat>

<Seat>
<ColumnNo>1</ColumnNo> 
<Deck>1</Deck> 
<Height>2</Height> 
<IsAvailable>1</IsAvailable> 
<RowNo>0</RowNo>
<SeatLabel>SL5</SeatLabel> 
<Width>1</Width>
</Seat>

<Seat>
<ColumnNo>2</ColumnNo> 
<Deck>1</Deck> 
<Height>1</Height> 
<IsAvailable>1</IsAvailable> 
<RowNo>0</RowNo>
<SeatLabel/> 
<Width>1</Width>
</Seat>


<Seat>
<ColumnNo>3</ColumnNo> 
<Deck>1</Deck> 
<Height>2</Height> 
<IsAvailable>1</IsAvailable> 
<RowNo>0</RowNo>
<SeatLabel>SL1</SeatLabel> 
<Width>1</Width>
</Seat>

我已经制作了布局的图像,我想在其中点击空位并禁用可用的座位为 0( <IsAvailable>0</IsAvailable> )

在此处输入图像描述

我很困惑我应该如何制作下面的布局,然后用我得到的 XML 数据填充。

  • 如何制作座位布局。
  • 如何使用 XML 数据填充座位以及单击时将转移到下一个活动以进行进一步处理。
  • 根据其可用性启用禁用席位。

我能够在我的代码中解析来自 XML 的数据/值....我想知道如何制作布局格式并将这些 XML 值适合特定的座位。

4

2 回答 2

1

我就是这样做的,它仍处于测试阶段,我正在努力改进它。用户可以动态分配行数和座位数(见文本数据)

Github上提供了 Pre Beta 代码

在此处输入图像描述 在此处输入图像描述

在此处输入图像描述

带有测试数据的总线布局活动

package com.serveroverload.busbooking;

import com.example.busbooking.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class BusLayoutActivity extends Activity {

    // Test Data Inputs
    private final int NUMBER_OF_SEATS_LEFT_UPPER = 5;
    private final int NUMBER_OF_SEATS_RIGHT_UPPER = 5;
    private final int NUMBER_OF_ROWS_LEFT_UPPER = 1;
    private final int NUMBER_OF_ROWS_RIGHT_UPPER = 2;

    private final int NUMBER_OF_SEATS_LEFT_LOWER = 8;
    private final int NUMBER_OF_SEATS_RIGHT_LOWER = 7;
    private final int NUMBER_OF_ROWS_LEFT_LOWER = 1;
    private final int NUMBER_OF_ROWS_RIGHT_LOWER = 3;

    private Double totatCost = 0.0;
    private int totalSeats = 0;
    private TextView totalPrice;
    private TextView totalBookedSeats;

    private LayoutParams seatParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dynamic_bus_layout);

        // Fetch Required Layouts
        RelativeLayout busLayoutUpper = (RelativeLayout) findViewById(R.id.upper_deck);
        RelativeLayout busLayoutBottom = (RelativeLayout) findViewById(R.id.lower_deck);
        totalPrice = (TextView) findViewById(R.id.total_cost);
        totalBookedSeats = (TextView) findViewById(R.id.total_seats);

        // Layout Param for Seats
        seatParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        seatParams.weight = 1;
        seatParams.leftMargin = 5;
        seatParams.rightMargin = 5;

        // Add Bottom Seats
        addLeftSeats(NUMBER_OF_ROWS_LEFT_UPPER, NUMBER_OF_SEATS_LEFT_UPPER, busLayoutBottom);
        addRightSeats(NUMBER_OF_ROWS_RIGHT_UPPER, NUMBER_OF_SEATS_RIGHT_UPPER, busLayoutBottom);

        // Add Upper Seats
        addLeftSeats(NUMBER_OF_ROWS_LEFT_LOWER, NUMBER_OF_SEATS_LEFT_LOWER, busLayoutUpper);
        addRightSeats(NUMBER_OF_ROWS_RIGHT_LOWER, NUMBER_OF_SEATS_RIGHT_LOWER, busLayoutUpper);

    }

    /*
     * This function draw seats for right row
     */
    private void addRightSeats(int numberOfRowsRight, float numberOfSeatsInRow, ViewGroup busLayout) {
        int previousRow;

        // ADD RIGHT ROWS

        previousRow = 0;

        // Begin adding rows
        for (int rightRowCount = 0; rightRowCount < numberOfRowsRight; rightRowCount++) {

            // Adding Linear layout as row
            LinearLayout rightRow = new LinearLayout(getApplicationContext());
            rightRow.setGravity(Gravity.CENTER);
            rightRow.setId(100 + rightRowCount);

            // make seats equal distance
            rightRow.setWeightSum(numberOfSeatsInRow);

            // if it is first row add row to window (ALIGN_PARENT_BOTTOM)
            // else add row above window row (ABOVE)
            if (previousRow != rightRowCount) {
                RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                newParams.addRule(RelativeLayout.ABOVE, 100 + previousRow);
                newParams.setMargins(10, 10, 10, 10);
                rightRow.setLayoutParams(newParams);

            } else {

                // Layout params for first row (Window seat), adding margin and
                // bottom alignment
                RelativeLayout.LayoutParams rightRowParam = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                rightRowParam.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                rightRowParam.setMargins(10, 10, 10, 10);
                rightRow.setLayoutParams(rightRowParam);
            }

            // Add Seats in row we have added
            for (int rowCount = 0; rowCount <= numberOfSeatsInRow; rowCount++) {

                // Left Upper
                final Seat rightRowSeat = (Seat) LayoutInflater.from(this).inflate(R.layout.seat, null);
                rightRowSeat.setLayoutParams(seatParams);
                rightRowSeat.setSeatNumber("SL" + rowCount);
                rightRowSeat.setGravity(Gravity.CENTER);

                rightRowSeat.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        updateCost(rightRowSeat);

                        Toast.makeText(getApplicationContext(), "seat # " + rightRowSeat.getSeatNumber(), 300).show();

                    }
                });

                // add seat to row
                rightRow.addView(rightRowSeat);

            }

            // and then add row to bus layout
            busLayout.addView(rightRow);

            // update row counter
            previousRow = rightRowCount;

        }
    }

    /*
     * This function draw seats for left row
     */
    private void addLeftSeats(int numberOfRowsLeft, float nuumberOfSeatsInRow, ViewGroup busLayout) {
        // Adding Left side rows

        int previousRow = 0;

        for (int leftRowCount = 0; leftRowCount < numberOfRowsLeft; leftRowCount++) {

            // Adding Linear layout as row
            LinearLayout LeftRow = new LinearLayout(getApplicationContext());
            LeftRow.setGravity(Gravity.CENTER);
            LeftRow.setId(leftRowCount);

            // Equi distance seats
            LeftRow.setWeightSum(nuumberOfSeatsInRow);

            // if it is first row add row to window
            // else add row below window row (BELOW)
            if (previousRow != leftRowCount) {
                RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                newParams.addRule(RelativeLayout.BELOW, previousRow);
                newParams.setMargins(10, 10, 10, 10);
                LeftRow.setLayoutParams(newParams);

            } else {
                RelativeLayout.LayoutParams leftRowParam = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                leftRowParam.setMargins(10, 10, 10, 10);
                LeftRow.setLayoutParams(leftRowParam);
            }

            // Add Seats in row
            for (int rowCount = 0; rowCount <= nuumberOfSeatsInRow; rowCount++) {

                // Left Upper
                final Seat leftRowSeat = (Seat) LayoutInflater.from(this).inflate(R.layout.seat, null);
                leftRowSeat.setLayoutParams(seatParams);
                leftRowSeat.setSeatNumber("SL " + rowCount);
                leftRowSeat.setGravity(Gravity.CENTER);

                leftRowSeat.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        updateCost(leftRowSeat);

                        Toast.makeText(getApplicationContext(), "seat # " + leftRowSeat.getSeatNumber(), 300).show();
                    }
                });

                LeftRow.addView(leftRowSeat);

            }

            // add row to bus layout
            busLayout.addView(LeftRow);

            // update row counter
            previousRow = leftRowCount;

        }
    }

    private void updateCost(final Seat ud_LeftWindowSeat) {

        if (ud_LeftWindowSeat.setSelected()) {

            totatCost += ud_LeftWindowSeat.getSeatPrice();
            ++totalSeats;

        } else {
            totatCost -= ud_LeftWindowSeat.getSeatPrice();
            --totalSeats;
        }

        totalPrice.setText("" + totatCost);
        totalBookedSeats.setText("" + totalSeats);
    }
}

座位的自定义 TextView

package com.serveroverload.busbooking;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class Seat extends TextView {

    public Seat(Context context) {
        super(context);

    }

    public Seat(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public Seat(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    private String seatNumber;
    private Boolean isSelected = false;
    private Boolean isWomen = false;
    private Double seatPrice = 100.00;

    public Boolean getIsWomen() {
        return isWomen;
    }

    public void setIsWomen(Boolean isWomen) {
        this.isWomen = isWomen;
    }

    public Double getSeatPrice() {
        return seatPrice;
    }

    public void setSeatPrice(Double seatPrice) {
        this.seatPrice = seatPrice;
    }

    public Boolean getIsSelected() {
        return isSelected;
    }

    public Boolean setSelected() {

        this.isSelected = !this.isSelected;

        if (getIsSelected()) {
            setBackgroundColor(0xff99cc00);

        } else {
            setBackgroundColor(0xff33b5e5);

        }

        return isSelected;

    }

    public String getSeatNumber() {
        return seatNumber;
    }

    public void setSeatNumber(String seatNumber) {
        this.seatNumber = seatNumber;

        setText(seatNumber);
    }

}

活动布局

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFF"
        android:orientation="vertical"
        android:weightSum="2"
        tools:context="com.serveroverload.busbooking.MainActivity" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:text="Lower Deck"
            android:textColor="#F00"
            android:textSize="20sp" />

        <RelativeLayout
            android:id="@+id/upper_deck"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@drawable/bus_outline"
            android:minHeight="150dp" >
        </RelativeLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:text="Upper Deck"
            android:textColor="#F00"
            android:textSize="20sp" />

        <RelativeLayout
            android:id="@+id/lower_deck"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@drawable/bus_outline"
            android:minHeight="150dp" >
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/total_seats_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="Number Of Seats"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/total_seats"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_gravity="center_horizontal"
                android:gravity="center_horizontal"
                android:textColor="#0F0"
                android:textSize="20sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/total_cost_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="Total Cost"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/total_cost"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:textColor="#0F0"
                android:textSize="20sp" />
        </RelativeLayout>
    </LinearLayout>

</ScrollView>

用于布局充气 seat.xml 的简单座位布局

<com.serveroverload.busbooking.Seat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    android:background="@android:color/holo_blue_light"
    android:gravity="center"
    android:textColor="#000"
    android:textSize="20sp" />

总线轮廓形状可绘制 bus_outline.xml

<corners
    android:bottomLeftRadius="0dp"
    android:bottomRightRadius="0dp"
    android:radius="2dp"
    android:topRightRadius="0dp" />

<stroke
    android:width="2dp"
    android:color="@android:color/holo_orange_dark" />

于 2016-05-05T08:43:55.080 回答
1

使用表格布局进行设计。您可以使用此代码进行设计参考此链接

在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:shrinkColumns="*"
        android:stretchColumns="*">

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:layout_span="4"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="Table 2"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="A"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="B"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="C"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="D"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="E"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="F"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="G"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="H"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="I"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="J"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="K"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="L"
            android:textColor="#000000" />
    </TableRow>
    </TableLayout>
</LinearLayout>
于 2016-05-05T03:19:40.247 回答