简体   繁体   English

如何在我的Android应用程序中实现这样的布局?

[英]How do I achieve a layout like this in my Android app?

I am a novice android programmer. 我是新手android程序员。 I am designing my first app, which is a mp3 player. 我正在设计我的第一个应用程序,这是一个MP3播放器。 I wish to achieve a layout like the one below 我希望实现如下所示的布局

https://m2.behance.net/rendition/pm/12717697/disp/05ec216e30ef24ec7a2cac85a5329140.jpg https://m2.behance.net/rendition/pm/12717697/disp/05ec216e30ef24ec7a2cac85a5329140.jpg

This is my preliminary structure: 这是我的初步结构:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/content_frame"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="fill_parent"
    android:background="#33000000"
    android:layout_height="20dp"
    android:layout_weight="1"
    android:id="@+id/_header"
    android:layout_gravity="top"
    android:orientation="vertical"
    >



</LinearLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:background="#74000000"
    android:layout_height="40dp"
    android:layout_weight="2"
    android:id="@+id/_footer"
    android:layout_gravity="bottom"
    >



</RelativeLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:background="#1A000000"
    android:layout_height="478dp"
    android:layout_weight="3"
    android:id="@+id/_middle"
    android:layout_gravity="center"
    android:orientation="vertical"
    >



</LinearLayout>


</FrameLayout>

However I am not getting the desired effect. 但是我没有达到预期的效果。 The layouts are overlapping and I have no idea how to fix them. 布局是重叠的,我不知道如何解决它们。 I have tried RelativeLayouts, fragments and LinearLayouts. 我尝试过RelativeLayouts,片段和LinearLayouts。 But nothing is working. 但没有任何工作。

What should I do instead? 我该怎么做呢?

You need to create custom Navigation drawer . 您需要创建自定义导航抽屉 For Demo Purpose . 用于演示目的。 NavigationDrawer-MaterialDesign And JamsMusicPlayer NavigationDrawer-MaterialDesignJamsMusicPlayer

To get you started, use: 为了帮助您入门,请使用:

  1. a RelativeLayout as the root. 一个RelativeLayout作为根。
  2. Header should be aligned at top: 标题应在顶部对齐:
    • android:layout_alignParentTop="true"
  3. Footer should be aligned at bottom: 页脚应在底部对齐:
    • android:layout_alignParentBottom="true"
  4. Body should be aligned between Header and Footer 正文应该在页眉和页脚之间对齐
    • android:layout_above="@+id/footer"
    • android:layout_below="@+id/header"

This yields a layout like the following: 这会产生如下布局:

在此输入图像描述

The same pattern can be used inside the Header and Footer to align the items in there, except this time you would align to the right and left of the parent instead of top and bottom. 可以在页眉和页脚内部使用相同的模式来对齐那里的项目,除非这次您将对齐父项的右侧和左侧而不是顶部和底部。 As for the NavigationDrawer, Google has some documents on this here . 至于NavigationDrawer,谷歌在这里有一些文件。

NOTE: You could also use nested LinearLayout but this is known to give bad performance ( see here ). 注意:您也可以使用嵌套的LinearLayout但这会导致性能不佳( 请参阅此处 )。


Code

This xml layout that produced the above image is here: 生成上述图像的xml布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="HEADER"
            android:textAppearance="@android:style/TextAppearance.Large"
            android:layout_centerInParent="true"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/footer"
        android:layout_below="@+id/header">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="BODY"
            android:textAppearance="@android:style/TextAppearance.Large"
            android:gravity="center"/>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="FOOTER"
            android:textAppearance="@android:style/TextAppearance.Large"
            android:layout_centerInParent="true"/>
    </RelativeLayout>

</RelativeLayout>

If you want to have a menu (default is hidden, and user can swipe to show it), you should use a DrawerLayout as root element. 如果您想要一个菜单​​(默认是隐藏的,用户可以滑动以显示它),您应该使用DrawerLayout作为根元素。 Guide here . 在这里指导。

The second root element should be a LinearLayout , with vertical orientation. 第二个根元素应该是LinearLayout ,具有垂直方向。 This layout will contain 3 other LinearLayout layouts: 此布局将包含3个其他LinearLayout布局:

The first one (orientation to horizontal) contains menu icon, title, 3 dots... 第一个(水平方向)包含菜单图标,标题,3个点......

The second one (orientation to vertical) contains contains album image, singer name... 第二个(垂直方向)包含专辑图片,歌手姓名......

The last one (orientation to horizontal) contains play, pause, next... button 最后一个(水平方向)包含播放,暂停,下一个...按钮

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

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