简体   繁体   中英

Android Have a Table and buttons on the same layout xml

I've got a page where I want to put a table in the middle and have buttons above and below. I've got the following XML for my layout. But even I can work out it won't work!

Edit: I've updated my XML to show what I want to achive. The tables inside, however it wont let me access by ListView listView = (ListView) findViewById(R.id.lvLocation);

在此处输入图片说明

    <RelativeLayout 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" >

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Add Item" />

       <ListView
           android:id="@+id/lvLocation"
           android:layout_width="match_parent"
           android:layout_height="fill_parent"
           android:layout_above="@+id/btnBack"
           android:layout_alignParentLeft="true"
           android:layout_below="@+id/btnAdd"
           android:drawSelectorOnTop="false" >

    </ListView>

    <Button
        android:id="@+id/btnBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="Back" />

</RelativeLayout>

My code: (with the Db call and array setup taken out)

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location_list_view);


    ListView listView = (ListView) findViewById(R.id.lvLocation);
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_2, android.R.id.text1, mArrayList);

    int[] colors = {0, 0xFFFF0000, 0}; 
    listView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
    listView.setDividerHeight(1);
    listView.setAdapter(adapter); 

    }



}

Heres what i'm trying to achieve. Not the colours, just the concept:

在此处输入图片说明

I think this will help

<RelativeLayout 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" >

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Add Item" />

    <TableLayout
        android:id="@+id/lvLocation"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/btnBack"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/btnAdd" >

    </TableLayout>

    <Button
        android:id="@+id/btnBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="Back" />

</RelativeLayout>

Instead of :

ListView listView = (ListView) findViewById (R.id.lvlocation);

Just use:

 ListView listView  = getListView();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM