如果采用属性layout_below,如何向recyclerView添加按钮?

时间:2015-03-25 16:48:25

标签: android relativelayout android-recyclerview

我的activitymain.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include android:id="@+id/app_bar" layout="@layout/toolbar"/>

<view
    android:id="@+id/recycler_view"
    android:layout_below="@id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="android.support.v7.widget.RecyclerView"/>   

</RelativeLayout>

因为我的main.java文件只包含Recyclerview而没有其他内容,所以我不能直接在其中添加2个按钮。

我尝试在recyclerView中添加2个imageButtons。

RecyclerView无法在AndroidStudio中正确呈现自己 - 谷歌存在问题。

所以我无法看到回收者视图和工具栏(app_bar)如何相互支持(我假设app_bar工具栏位于recyclerview(layout_below属性)之下)

我怀疑我应该完全改变应用程序的设计来完成它。

MainActivity.java,它只有recyclerview和适配器,它将上下文发送到下一个SearchActivity:

public class MainActivity extends BaseActivity {

private static final String LOG_TAG = "mAIN ACTIVITY";
private List<Book> bookList = new ArrayList<>() ;
private RecyclerView recyclerView;
private BookRecyclerViewAdapter bookRecyclerViewAdapter;

private boolean FIRST_TIME_LAUNCH = false ;

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        bookRecyclerViewAdapter = new BookRecyclerViewAdapter(new ArrayList<Book>(), MainActivity.this);
        recyclerView.setAdapter(bookRecyclerViewAdapter);

        }

如果我尝试添加按钮,则它位于左上角:

kk

2 个答案:

答案 0 :(得分:1)

如果您希望按钮在列表向下滚动后在列表的末尾显示,请查看this问题。

如果您希望按钮位于列表中并始终可见,请按以下步骤操作: 您可以在按钮和RecyclerView之间插入Toolbar,为其提供layout_below layout_above属性:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include android:id="@+id/app_bar" layout="@layout/toolbar"/>

    <Button
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <view
        android:id="@+id/recycler_view"
        android:layout_below="@id/app_bar"
        android:layout_above="@id/button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="android.support.v7.widget.RecyclerView"/>

</RelativeLayout>

答案 1 :(得分:1)

试试这个:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include android:id="@+id/app_bar" layout="@layout/toolbar"/>

<view
    android:id="@+id/recycler_view"
    android:layout_below="@id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    class="android.support.v7.widget.RecyclerView"/>

<Button
    android:id="@+id/button"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_below="@id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />


</RelativeLayout>

希望这有帮助!