【问题标题】:Adding a Button below a ListView in a ListActivity在 ListActivity 中的 ListView 下方添加按钮
【发布时间】:2021-05-10 15:12:24
【问题描述】:

我需要一个 ListActivity 和一个 ListView,它下面有一个 Button。因此,您可以在 Button 停留在底部时滚动 ListView。它在布局中看起来不错,但是当我启动应用程序时,没有按钮。 那是我的 ListActivity 的 XML:

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

这是pallen_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>

这就是我在 ListActivity 的 onCreate() 方法中将 onClickListener 添加到按钮的方式:

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();
                            }
                        }

                    });

这是我的布局,按钮可见: Layout of my ListActivity

为什么我启动应用程序时按钮不可见? 我很高兴有任何帮助!

问候 莫里斯 F.

【问题讨论】:

    标签: java android xml android-layout listactivity


    【解决方案1】:

    SirGrey 最终是对的。

    那一定是你的代码有问题...但是我看不到问题,如果活动什么也没显示,一定是充气机的问题... – SirGery

    问题是充气机,所以我对我的代码进行了以下更改:

    • 我从 MainActivity 的 XML 中删除了 android:entries="@layout/paletten_item"

    • MainActivity 现在是extends Activity,不再是ListActivity

    • setContentView() 在我的 MainActivity 的 onCreate() 方法中

    • 我通过 listViewPaletten = (ListView)findViewById(R.id.listViewPaletten); 获得 ListView

    • 而且我不再使用 Infalter。

    非常感谢您的帮助!

    【讨论】:

    • 你的活动没有扩展AppCompatActivity吗?我以为你有它。我很高兴我能以某种方式“帮助”您解决问题!
    • 不,这就是我称之为 ListActivity 的原因:D
    • 我对你的问题感到很困惑,而你甚至没有那个扩展...... :´) 无论如何问题已经解决了,所以我们很好!
    【解决方案2】:

    如果您想滚动ListView同时按钮保持在同一位置,请尝试使用FloatingActionButton,它一定可以解决您的问题

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

    【讨论】:

    • 感谢您的快速回答!这对我没有用,因为我的 IDE 不知道 FloatingActionButton 并且在我编译应用程序时会导致错误
    • 你的应用程序@MorrisF 上运行的是哪个安卓版本?
    • 尝试将此实现添加到您的build.gradle 的依赖项implementation 'com.google.android.material:material:1.1.0' 如果那个不起作用,请尝试以下implementation 'com.google.android.material:material:1.3.0' implementation 'com.google.android.material:material:1.2.1' implementation 'com.google.android.material:material:1.0.0-rc01'
    • 我知道这很难过...但我需要告诉您,我正在使用带有 Eclipse Kepler 作为 IDE 的 Android 18...所以我的项目中没有 build.gradle 文件。我知道 Android Studio 要好得多,但我正在编程的应用程序非常大而且在某些部分非常旧。我尝试将其迁移到 Android Studio 中但多次失败...所以我必须使用 Kepler...
    【解决方案3】:

    然后试试这个,交换引用的活动,因为这是来自我的项目

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

    【讨论】:

    • 我尝试了你所说的一切,但没有任何改变......?当我设置 android:layout_height="match_parent" 时,整个布局页面都充满了按钮,但是当我启动应用程序时,只有我的 ListView 没有按钮。老实说,我不明白
    • 它应该像这样工作......在我的一个项目中尝试过并且工作......,我已经用完整的布局编辑了上面的代码,试试吧。
    • 布局现在看起来很好但是再次:当我启动应用程序时,我的 ListView 下方没有按钮
    • 这一定是你的代码有问题...但是我看不到问题,如果活动没有显示任何内容,则必须与充气机有关...
    猜你喜欢
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    相关资源
    最近更新 更多