【问题标题】:Button over a preferences ListView in ICS and Jelly Bean (API 15 / 16)ICS 和 Jelly Bean 中的首选项 ListView 上的按钮 (API 15 / 16)
【发布时间】:2016-05-11 21:57:59
【问题描述】:

我加载具有以下布局的首选项屏幕:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/export_main_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/windowBackground"
    android:clickable="true">

    <ImageView
        android:id="@+id/export_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/entry_background" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/export_settings_list"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/export_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_file_upload_white_24"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:clickable="true"/>

</RelativeLayout>

RecyclerView 是扩展 XML 首选项文件的列表。其余的布局,FAB 和 ImageView,只是为了自定义首选项屏幕而添加的。

所有这些都在最新的 Android 版本上运行良好,但在 ICS 和 Jelly Bean(我没有尝试过 Kikat)上,FloatingActionButton 无法接收任何点击并且变得有点透明。

我尝试了十几种不同的方法(使用标准按钮、标准 lisview 等),但按钮在屏幕上仍然无用,总是单击下面的列表项。

我认为问题可能来自于我膨胀了一个 XML 首选项文件,因为我使用标准列表创建了相同的布局,并且该按钮在任何版本中都可以正常工作。

最坏的情况我只会为旧版本加载一个特定的布局......

【问题讨论】:

    标签: android listview


    【解决方案1】:

    其实我终于成功了。

    在我的问题中,我从 XML 添加按钮,但无法捕获 on click 事件。我终于改了代码,在布局中动态添加了按钮,果然奏效了!

    我仍然无法解释为什么在以编程方式添加按钮时可以单击,也许使用 XML 按钮仍保留在列表下方...

    代码如下:

        final ViewGroup innerContainer = (ViewGroup) exportView.findViewById(R.id.export_main_frame);
        final View innerView = super.onCreateView(inflater, innerContainer, savedInstanceState);
        if (innerView != null) {
            innerContainer.addView(innerView);
    
            // Add dynamically the FAB to export
            FloatingActionButton dynamicExportFAB = new FloatingActionButton(getActivity());
            dynamicExportFAB.setImageResource(R.drawable.ic_file_upload_white_24);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.ALIGN_PARENT_END);
    
            dynamicExportFAB.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //TODO
                }
            });
    
            innerContainer.addView(dynamicExportFAB, params);
        }
    

    【讨论】:

      猜你喜欢
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多