如何在XML的Fragment中的ListView上方放置浮动操作按钮(FAB)? [英] How do you put a floating action button (FAB) above a ListView in a Fragment in xml?
问题描述
这是我的XML,但是它抱怨我有两个根".
This is my XML but it complains that I have "two roots"..
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@android:id/list"
style="@style/ListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add_white_48dp"
android:layout_gravity="bottom|end"
fab:elevation="6dp"
fab:pressedTranslationZ="12dp"
fab:backgroundTint="@color/accent"/>
如果我将它们都包装在另一个根目录(例如RelativeLayout或FrameLayout)中,则我的应用程序将崩溃...
If I wrap them both in another root such as RelativeLayout or FrameLayout my app crashes...
原因:java.lang.ClassCastException:android.widget.FrameLayout 无法转换为android.widget.ListView
Caused by: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.widget.ListView
我的ListView是由片段中的适配器填充的,就像这样...
My ListView is populated by an adapter in my Fragment like so...
ListView mListView = (ListView) inflater.inflate(R.layout.main_list_fragment, container, false);
FloatingActionButton myFab = (FloatingActionButton) mListView.findViewById(R.id.fab);
myFab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//// TODO: 23/09/15
}
});
int[] to = new int[]{R.id.item};
String[] columns = new String[]{"name"};
listAdapter = new CustomAdapter(this.getActivity(), null, columns, to);
this.setListAdapter(listAdapter);
如何构造XML,以使FAB悬浮在ListView上方?
How do I structure my XML so that I have my FAB floating above the ListView?
推荐答案
您可以按以下方式使用CoordinatorLayout
布局.
您在上方列表视图中的含义是什么,如果您在列表顶部,则必须更改android:layout_gravity="top"
You can use CoordinatorLayout
layout as follow.
What do you meant by above list view, if you meant at top of list then you have to change android:layout_gravity="top"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@color/white"
android:divider="@null"
android:orientation="vertical">
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@android:id/list"
style="@style/ListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add_white_48dp"
android:layout_gravity="bottom|end"
fab:elevation="6dp"
fab:pressedTranslationZ="12dp"
fab:backgroundTint="@color/accent"/>
</android.support.design.widget.CoordinatorLayout>
这篇关于如何在XML的Fragment中的ListView上方放置浮动操作按钮(FAB)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!