繁体   English   中英

修复底部的线性布局作为 android 中的页脚

[英]fix linearlayout at the bottom as a footer in android

我做了一个简单的布局xml文件。 我一直在一点一点地解决我的问题。 我正在使用 scrolview、linearlayout 和 tableview。 我有一个顶栏,它被锁定在顶部。 它从不移动。 在中间,我有一个滚动视图,我需要将最后一个线性布局作为页脚放在底部,如果用户向下滚动,我不希望它消失。 我想把它锁在那里。

这是我的代码

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

   <TableLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent">

         <TableRow
        android:background="@drawable/gradient"
        android:gravity="center">

            .......

         </TableRow>

   </TableLayout>

    <ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/sw_layout"
android:orientation="vertical">

      <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical" >

    <TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#EEEEEE" >

        <TableRow
        android:id="@+id/tableRowDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

            ..........

            </TableRow>

        </TableLayout>

        /*****************************************/
        /* I want this to be footer at the bottom*/
        /*****************************************/
        <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_date"
            android:layout_height="35dp"
            android:padding="5dp"
            android:drawableLeft="@drawable/calendar"
            android:text="Tarih" />

            <Button
            android:id="@+id/btn_converter"
            android:layout_height="35dp"
            android:padding="5dp"
            android:drawableLeft="@drawable/calendar"
            android:text="Hesap" />
          </LinearLayout>
        /*****************************************/
        /* I want this to be footer at the bottom*/
        /*****************************************/

       </LinearLayout>
     </ScrollView>  
 </LinearLayout>

如何才能将页脚锁定在底部?

你应该使用RelativeLayout并在页脚布局中添加标签: android:layout_alignParentBottom="true" ; 对于你的ScrollView ,你应该把它放在页脚布局上面( android:layout_above="@+id/idOfYourFooterLayout"

尝试这个 :

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

  <ScrollView android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:id="@+id/sw_layout"
       android:layout_above="@+id/footer"
       android:orientation="vertical">
//your UI...
  </ScrollView>

   <LinearLayout
      android:id="@+id/footer"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:orientation="vertical" >

      <Button
          android:id="@+id/btn_date"
          android:layout_height="35dp"
          android:padding="5dp"
          android:drawableLeft="@drawable/calendar"
          android:text="Tarih" />

      <Button
          android:id="@+id/btn_converter"
          android:layout_height="35dp"
          android:padding="5dp"
          android:drawableLeft="@drawable/calendar"
          android:text="Hesap" />
   </LinearLayout>
</RelativeLayout>

您可以设置页眉,页脚和ScrollViewlayout_height="0dp"并定义layout_weight 只需使用这些值,直到找到最适合的值。 由此产生的页眉和页脚高度会随屏幕大小而动态变化。

值1,5,1表示第一个元素占父LinearLayout可用高度的1/7,第二个5/7和最后1/7。

尝试

<!-- HEADER -->
<TableLayout
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:layout_weight="1" >

<!-- BODY -->
<ScrollView
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:id="@+id/sw_layout"
   android:orientation="vertical"
   android:layout_weight="5">

<!-- FOOTER -->
<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:orientation="vertical"
   android:layout_weight="1" >

像这样使用一个xml - >>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


</LinearLayout>

并将其包括在内

LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

  ...

  <!-- Overridden attributes never work. Nor do attributes like
       the red background, which is specified here. -->
  <include
      android:id="@+id/buttons_override"
      android:background="#ff0000"
      android:layout_width="fill_parent"
      layout="@layout/buttons"/>

</LinearLayout>

使用父布局作为RelativeLayout并将此属性android:layout_alignParentBottom="true"到页脚布局,并将LinearLayout(页脚布局)放在scrollview之外。 像这样

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

   <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"  //// add this property
      android:orientation="vertical" >

      <Button
          android:id="@+id/btn_date"
          android:layout_height="35dp"
          android:padding="5dp"
          android:drawableLeft="@drawable/calendar"
          android:text="Tarih" />

      <Button
          android:id="@+id/btn_converter"
          android:layout_height="35dp"
          android:padding="5dp"
          android:drawableLeft="@drawable/calendar"
          android:text="Hesap" />
   </LinearLayout>

使用相对布局作为父布局,然后添加三个子视图,如下所示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" >
    <View android:id="@+id/top_view"
     android:alignParentTop="true">  // Your view that as to be in Top 
    </View> 

     <ScrollView android:layout_below ="@id/top_view" > //scroll view in middle below topview
     </ScrollView>

    <View android:alignParentBotton="true">  //Your expected view in bottom
    </View>

</RelativeLayout>

就我而言,我在 Oncreate 函数和布局顶部添加了以下这些代码行

像下面

在此处输入图片说明

代码在这里:

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)

暂无
暂无

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

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