Android 安卓按钮永远在底部
我有下面的Xml代码,我想在FrameLayount中显示片段的内容,但我不知道如何将按钮始终保持在底部Android 安卓按钮永远在底部,android,xml,layout,Android,Xml,Layout,我有下面的Xml代码,我想在FrameLayount中显示片段的内容,但我不知道如何将按钮始终保持在底部 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_lista_preguntas" android:layout_width="match_par
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_lista_preguntas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ingenierovagabundo.encuestafacil.GUI.ListaPreguntasActivity">
<!-- Toolbar -->
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/appbar_nueva_pregunta"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="@+id/contenedor_Pregunta"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</FrameLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:text="@string/npa_button_guardar"/>
使用LinearLayout作为容器,然后元素彼此跟随
如果您想让按钮始终位于底部,请使用带有的RelativeLayout
android:layout\u alignParentBottom=true
您还需要关闭您的容器 在activity_main.xml中使用此代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:backgroundTint="@color/colorOrange"
app:srcCompat="@android:drawable/ic_menu_share"
tools:ignore="VectorDrawableCompat" />
</android.support.design.widget.CoordinatorLayout>
在content_main.xml中,使用以下代码
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="@layout/app_bar_main">
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
您可以使用相对线代替线性布局,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_lista_preguntas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ingenierovagabundo.encuestafacil.GUI.ListaPreguntasActivity">
<!-- Toolbar -->
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/appbar_nueva_pregunta"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="@+id/contenedor_Pregunta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn"
android:layout_below="@+id/appbar_nueva_pregunta">
</FrameLayout>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:text="@string/npa_button_guardar" />
您可以将android:layout\u weight=1添加到框架布局中。这将展开FrameLayout以填充父视图中的任何剩余空间。这将导致按钮位于底部。
因此,生成的框架布局将是
<FrameLayout
android:id="@+id/contenedor_Pregunta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
</FrameLayout>
有关布局重量的更多信息,请访问链接