简体   繁体   中英

Adding a Button below a ListView in a ListActivity

I need a ListActivity with a ListView that has a Button below it. So you can scroll the ListView while the Button stays on the bottom. It looks fine in the layout but when i launch the app, there is no Button. That is the XML of my ListActivity:

<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"
    android:descendantFocusability="blocksDescendants"
    tools:context=".PalettenActivity" >

    <ListView
        android:id="@+id/listViewPaletten"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/buttonPalettenBetaetigen"
        android:entries="@layout/paletten_item"
        android:minHeight="?android:attr/listPreferredItemHeight" >

    </ListView>

    <Button
        android:id="@+id/buttonPalettenBetaetigen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="Bestätigen" />

</RelativeLayout>

Here's the code of the paletten_item:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layoutPalettenItem"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    tools:context=".ChecklistAdapter" >
    
    <TextView
        android:id="@+id/textViewPalettenBez"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:gravity="left|center_vertical"
        android:text="Bezeichnung"
        android:textSize="25sp" />

    <Button
        android:id="@+id/buttonPalettenPlus"
        android:layout_width="65dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:text="+"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textViewPalettenAnzahl"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:layout_alignBaseline="@+id/buttonPalettenPlus"
        android:layout_alignBottom="@+id/buttonPalettenPlus"
        android:layout_toLeftOf="@+id/buttonPalettenPlus"
        android:gravity="center"
        android:text="0"
        android:textSize="20sp" />

    <Button
        android:id="@+id/buttonPalettenMinus"
        android:layout_width="65dp"
        android:layout_height="match_parent"
        android:layout_alignBaseline="@+id/textViewPalettenAnzahl"
        android:layout_alignBottom="@+id/textViewPalettenAnzahl"
        android:layout_toLeftOf="@+id/textViewPalettenAnzahl"
        android:text="-"
        android:textSize="20sp" />
</RelativeLayout>

This is how i add the onClickListener to the Button in the onCreate() method of the ListActivity:

LayoutInflater itemInflater (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = itemInflater.inflate(R.layout.activity_paletten, null);

btnPalettenBestaetigen = (Button)convertView.findViewById(R.id.buttonPalettenBetaetigen);
btnPalettenBestaetigen.setText("Bestätigen");
btnPalettenBestaetigen.setVisibility(View.VISIBLE);
btnPalettenBestaetigen.setEnabled(true);
btnPalettenBestaetigen.setClickable(true);
btnPalettenBestaetigen.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try {
                                // do stuff
                            } catch (Exception e) {
                                Log.e(TAG, e.getMessage());
                                e.printStackTrace();
                            }
                        }

                    });

Here is my layout with the button visible: Layout of my ListActivity

Why isn`t the Button visible when i launch the app? I am glad for any help!

Greetings Morris F.

SirGrey was right in the end.

That must be a problem with your code... But i can't see the issue, if the activity shows nothing, must be something with with the inflater... – SirGery

The Problem was the Inflater so i did the following changes to my code:

  • I deleted android:entries="@layout/paletten_item" from the XML of my MainActivity

  • The MainActivity now extends Activity and no more ListActivity

  • I setContentView() in my onCreate() method of my MainActivity

  • I get the ListView by doing listViewPaletten = (ListView)findViewById(R.id.listViewPaletten);

  • and I dont use the Infalter anymore.

Thanks a lot for your Help!

If you want to scroll your ListView meanwhile the button stays in same position, try to use FloatingActionButton , it must solve your issue

<ListView
    android:id="@+id/listViewPaletten"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:entries="@layout/paletten_item"
    android:minHeight="?android:attr/listPreferredItemHeight" >

</ListView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fActionButton"
    android:layout_width="180dp"
    android:layout_height="180dp"
    tools:ignore="MissingConstraints,VectorDrawableCompat"/>

Then try this, swap the activity that makes reference, cause this is from my project

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.MainActivities.CartActivity">





<ListView
    android:id="@+id/listViewPaletten"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="1dp"
    tools:layout_editor_absoluteY="1dp"
    android:layout_above="@+id/buttonPalettenBetaetigen">



</ListView>
<Button
    android:id="@+id/buttonPalettenBetaetigen"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal"
    android:layout_alignParentBottom="true"
    android:text="Bestätigen" />

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