繁体   English   中英

如何在Android Studio中向视图分页器添加按钮?

[英]How to add button to a view pager in Android Studio?

我想向“查看寻呼机”添加按钮。 尝试添加按钮,片段和布局失败。 我也尝试过这样: 向ViewPager添加按钮,但是没有用。

这是我的布局

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.airpal.yonatan.airpal.MainActivity_User">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/appBarLayout">
        <include layout="@layout/app_bar_layout" android:id="@+id/main_page_toolbar" />

        <android.support.design.widget.TabLayout
            android:id="@+id/main_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabSelectedTextColor="@android:color/white"
            app:tabTextColor="@android:color/white">

        </android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/main_tabPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/appBarLayout">

    </android.support.v4.view.ViewPager>




</LinearLayout>

图片

这是屏幕截图,我想在页面底部添加按钮。 谢谢。

尝试对根视图使用RelativeLayout并使用:

android:layout_alignParentBottom="true"

在根视图中无法使用LinearLayout。 另外,ConstraintLayout代替RelativeLayout是一个很好的选择。

您可以在相对布局中包装此LinearLayout。 如果您不想将线性布局更改为RelativeLayout。 这是修改后的布局XML,它将在右下角显示Button。 您可以向左修改layout_alignParentRight,居中取决于您的需要。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout 
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.airpal.yonatan.airpal.MainActivity_User">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/appBarLayout">
            <include layout="@layout/app_bar_layout" android:id="@+id/main_page_toolbar" />

            <android.support.design.widget.TabLayout
                android:id="@+id/main_tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabSelectedTextColor="@android:color/white"
                app:tabTextColor="@android:color/white">

            </android.support.design.widget.TabLayout>
        </android.support.design.widget.AppBarLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/main_tabPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/appBarLayout">

        </android.support.v4.view.ViewPager>
    </LinearLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM