Как сделать Android xml Linerlayout в нижней части активности в Android Studio? - PullRequest
3 голосов
/ 02 мая 2020

Как мне сделать LinearLayout в Android Studio внизу страницы. Результат из кода ниже

Я хочу, чтобы выделенный затемненный макет находился внизу страницы. I want the highlighted darken layout to be at the bottom of the page.

<?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"

Ответы [ 3 ]

1 голос
/ 02 мая 2020

Вы можете использовать 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>
0 голосов
/ 03 мая 2020

Приведенный ниже подход поможет вам достичь ваших проектных характеристик с нулевыми перекрытиями. В основном, есть четыре ключевых шага:

  1. Измените ваш root макет на RelativeLayout .

  2. Внутри RelativeLayout , добавьте LinearLayout , который будет содержать вашу кнопку и TextView .

  3. Внутри того же RelativeLayout , добавьте LinearLayout , который вы собираетесь прикрепить к нижней части экрана.

  4. Убедитесь, что вы установили первый LinearLayout поверх второй с использованием атрибута android:layout_above="".

Теперь вот код:

<?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>

Надеюсь, это поможет.

0 голосов
/ 03 мая 2020

Используйте Frame Layout как базовый макет, чтобы сделать это наилучшим образом, это будет работать определенно. Просто используйте layout_gravity = "bottom" в LinearLayout, который вы хотите внизу, и просто так:

  <?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>
...