相对布局中的Listview变得透明

时间:2014-05-01 02:44:59

标签: android android-layout listview

我有一个列表视图,当设置为顶视图时显示其文本为黑色,但当嵌套在另一个布局(如RelativeLayout)时,其文本变为白色/透明。我尝试将其置于线性布局中,但行为没有改变。

以下是xml文件:

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

<Button
    android:id="@+id/purchaseBtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/list_view"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="78dp"
    android:text="@string/purchase" />

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/header" >
</ListView>

<Button
    android:id="@+id/menuReturnBtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/purchaseBtn"
    android:layout_centerHorizontal="true"
    android:text="@string/productMenu" />

<TextView
    android:id="@+id/header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/checkoutHeader"
    android:textAppearance="?android:attr/textAppearanceLarge" />

    </RelativeLayout>

以下是设置列表视图的活动:

    public class Checkout extends Activity implements OnItemClickListener {

        private MasterDatabaseAdapter db;
        private Button checkoutBtn;
        private Button menuReturnBtn;
        private ListView list;
        private List<String> orderArray;
        private StringTokenizer tokens;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.checkout_orders);

            // initiate database
            db = new MasterDatabaseAdapter(getApplicationContext());

            checkoutBtn = (Button) findViewById(R.id.checkoutBtn);
            menuReturnBtn = (Button) findViewById(R.id.menuReturnBtn);
            list = (ListView) findViewById(R.id.list_view);

            // setup order list
            orderArray = new ArrayList<String>();
            getReceipt(); // fills up orderArray with receipt output
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, orderArray);
            list.setAdapter(adapter);
            list.setOnItemClickListener(this);

            // navigate to product menu
            menuReturnBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    db.closeDB();
                    Intent launchMenu = new Intent(Checkout.this,
                    ProductMenu.class);
                    startActivity(launchMenu);

                }
            });


        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                 long id) {

            String code;
            String viewString = ((TextView) view).getText().toString();
            tokens = new StringTokenizer(viewString);

            // iterate through list view text
            tokens.nextToken(); // skip code label
            code = tokens.nextToken(); // product code value

            // close database before navigating 
            db.closeDB();

            Intent launchOrderItem = new Intent(Checkout.this,
                    OrderItem.class);
            launchOrderItem.putExtra("code", code); // pass code to another activity
            startActivity(launchOrderItem); // launch new activity

       }

        // displays receipt of ordered items
        public List<String> getReceipt(){
            String temp = "";
            Product item = null;

            // fetch list of products
            List<Product> products = db.getAllProducts();
            Log.i("Checkout", "fetched all products + count=" + products.size());
            // get iterator for product list
            Iterator<Product> iterator = products.iterator();
            while (iterator.hasNext()){
                item = iterator.next();
                // format receipt display before outputting
                temp = "CODE: " +item.getCode() + " @R"+item.getPrice() +"  x"+db.countOrders(item.getCode());
                Log.i("Checkout", temp);
                orderArray.add(temp);
            }

            Log.i("Checkout", "Size of orderArray: "+orderArray.size());
            return orderArray;

        }

    }

以下是输出屏幕截图的链接: https://drive.google.com/file/d/0BwxIRxqhBpk3amlELVJ4SHZndWs/edit?usp=sharing

1 个答案:

答案 0 :(得分:0)

// try this way
<?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">

    <TextView
        android:id="@+id/menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Menu"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="5dp"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/basketBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="@string/basket" />

    <Button
        android:id="@+id/setupBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="@string/slaveSetupBtn" />

</LinearLayout>