在单击按钮时,我试图在 RecyclerView
之上膨胀 EditText
。但不是将 EditText 添加到布局的顶部,从而将 RecyclerView 向下推,而是将它简单地放置在 RecyclerView 的顶部,像这样覆盖它:
您可以看到“新文本”覆盖了 RecyclerView 的第一项。我怎样才能让它膨胀到 RecyclerView 之上,并向下推 RecyclerView,以容纳新添加的 EditText?
这是我要将 EditText 添加到的 FrameLayout 的 XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_my_frame_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
<com.melnykov.fab.FloatingActionButton
android:id="@+id/button_floating_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:src="@drawable/ic_action_new"
fab:fab_colorNormal="@color/primary_dark"
fab:fab_colorPressed="@color/primary" />
这是我的 FloatingActionButton 的 onClick 方法,我希望在其中添加 RecyclerView 上方的 EditText:
public void onClick(View v) {
ViewGroup parent = (ViewGroup) rootView.findViewById(R.id.fragment_my_frame_layout);
View view = LayoutInflater.from(getActivity()).inflate(R.layout.add_keyword_edittext, parent, true);
}
这是我添加的 EditText 的 XML:
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edit_text_above_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:background="#00000000"
android:textCursorDrawable="@null"
android:imeOptions="actionDone"
android:singleLine="true"
android:text="New Text" />
最佳答案
在框架布局中,所有内容都相互堆叠,如果您希望它位于回收器 View 之上,请制作您的布局 LinearLayout 并调用方法 LinearLayout.addView(child, index)
索引是 0
在你的情况下。
更新
因为你有 float 按钮你应该让你的布局像这样:
<FrameLayout>
<LinearLayout>
<EditText/>//witch will be added later by add view
<RecyclerView/>
</LinearLayout>
<Button/>
<FrameLayout>
此外,如果您希望编辑文本像标题一样工作,您可以使用此: https://stackoverflow.com/a/26573338/2127203
关于android - 在 FrameLayout 中的 RecyclerView 上方展开 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26620036/