【问题标题】:How to make Android xml Linerlayout at the bottom of the activity in Android Studio?如何在Android Studio的活动底部制作Android xml Linearlayout?
【发布时间】:2020-08-17 07:53:49
【问题描述】:

如何在页面底部的 Android Studio 中制作 LinearLayout。下面代码的结果

我希望突出显示的深色布局位于页面底部。

<?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"
    tools:context=".MainPage"
    android:orientation="vertical"
    android:background="#ffffff">

<Button
        android:id="@+id/taptoscan"
        android:layout_marginTop="40dp"
        android:layout_marginBottom="10dp"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:background="@drawable/barcode"
        android:layout_gravity="center"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tap to Scan"
        android:fontFamily="@font/antic"
        android:layout_gravity="center"
        android:textSize="18sp"/>

    <!--here is the layout i want to put at the bottom of the page-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/colorPrimaryDark"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:gravity="bottom">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    </RelativeLayout>
</LinearLayout>

在上面的代码中,我尝试了下面的,但没有成功。

android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:gravity="bottom"

【问题讨论】:

  • 使用外层布局作为相对布局
  • @Ticherhaz 我也试过了,它不起作用

标签: android xml android-studio android-linearlayout


【解决方案1】:

你可以在外面使用RelativeLayout。像这样,然后再添加一个新的 LinearLayout。

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    tools:context=".MainPage">

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

        <Button
            android:id="@+id/taptoscan"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_gravity="center"
            android:layout_marginTop="40dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/barcode" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:fontFamily="@font/antic"
            android:text="Tap to Scan"
            android:textSize="18sp" />
    </LinearLayout>

    <!--here is the layout i want to put at the bottom of the page-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:background="@color/colorPrimaryDark"
        android:gravity="bottom"
        android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>

【讨论】:

    【解决方案2】:

    使用框架布局作为基本布局以最好的方式做到这一点,它肯定会起作用 只需在您想要的 LinearLayout 中使用 layout_gravity="bottom" 和 就像这样:

      <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:orientation="vertical"
        tools:context=".MainPage">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <Button
                android:id="@+id/taptoscan"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:layout_gravity="center"
                android:layout_marginTop="40dp"
                android:layout_marginBottom="10dp"
                android:background="@drawable/barcode" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:fontFamily="@font/antic"
                android:text="Tap to Scan"
                android:textSize="18sp" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_gravity="bottom"
            android:background="@color/colorPrimaryDark"
            android:orientation="horizontal">
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        </LinearLayout>
    
    </FrameLayout>
    

    【讨论】:

      【解决方案3】:

      以下方法将帮助您实现零重叠的设计规范。基本上,有四个关键步骤:

      1. 将根布局更改为 RelativeLayout

      2. RelativeLayout 中,添加一个 LinearLayout,其中包含您的 Button 和您的 TextView

      3. 在同一个 RelativeLayout 中,添加您要固定到屏幕底部的 LinearLayout

      4. 确保您使用 android:layout_above="" 属性将第一个 LinearLayout 设置在第二个之上。

      现在,代码如下:

      <?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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainPage">
      
      
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#ffffff"
            android:layout_above="@+id/bottomView">
      
      
            <Button
                android:id="@+id/taptoscan"
                android:layout_marginTop="40dp"
                android:layout_marginBottom="10dp"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:background="@drawable/barcode"
                android:layout_gravity="center"/>
      
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tap to Scan"
                android:fontFamily="@font/antic"
                android:layout_gravity="center"
                android:textSize="18sp"/>
      
        </LinearLayout>
      
        <!--here is the layout i want to put at the bottom of the page-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:id="@+id/bottomView"
            android:background="@color/colorPrimaryDark"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom"
            android:gravity="bottom">
      
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </RelativeLayout>
      
        </LinearLayout>
      
      </RelativeLayout>
      

      我希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2016-07-30
        • 1970-01-01
        • 1970-01-01
        • 2016-08-15
        • 1970-01-01
        • 2021-12-06
        • 1970-01-01
        • 1970-01-01
        • 2012-01-30
        相关资源
        最近更新 更多