android - 在 Android Studio 中排列 xml 中的元素

标签 android xml drawerlayout

我用过

drawerlayout

在我的 xml 中。我可以在相对布局中正确对齐,但我挣扎这个抽屉布局。

无法使用我在相对布局中使用的大部分功能。

但我设法在我想要的地方对齐元素。但这给了我另一个问题。

• 在 Android Studio 的XML 预览中,它完美对齐

• 在模拟器(编译时)看不到元素 Textview 和Button。

• 当我在我的手机 中安装时,这些元素从底部开始略高于


可以看到下面三张图,

XML Preview Emulator (Nexus One) My own mobile (Micromax)


Q1。那么为什么我得到这些不同类型的对齐方式。

Q2。我如何编写代码才能使 UI 看起来在所有设备上都相同

Q3。我在我的XML 代码(在下面发布)中犯了什么错误,以及在所有设备中获得相同 UI 的替换代码是什么。

XML 代码:

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">
    <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"/>
    </android.support.design.widget.AppBarLayout>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/dbListrv"
            android:layout_width="match_parent"
            android:layout_height="450dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="55dp"
            />
        <EditText
            android:layout_width="300dp"
            android:layout_height="62dp"
            android:id="@+id/et"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="0dp"
            android:layout_marginTop="505dp"
            />
        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="62dp"
            android:text="New Button"
            android:id="@+id/addButton"
            android:padding="0dp"
            android:layout_marginLeft="300dp"
           android:layout_marginTop="505dp"
            android:layout_marginBottom="0dp" />
    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>

还请大家说一些灵活安排元素的小窍门。 提前致谢。 (:

最佳答案

我认为您的问题是因为您为 RecyclerView 使用了固定高度。

它在某些屏幕上比在其他屏幕上占用更多空间,从而产生您所看到的效果。

尝试将 RecyclerView 放入 RelativeLayout 并将其高度设置为 MATCH_PARENT。

同时使用 RelativeLayout 将您的按钮和 TextView 与屏幕底部对齐,这就是我认为您正在尝试实现的目标。

关于android - 在 Android Studio 中排列 xml 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34178339/

相关文章:

Android ExpandableListView 禁用分组点击效果

java - html相机视频流不显示在android webview中

android - 在 DrawerLayout 上禁用手势监听器

android - setDrawerLockMode 在 android 中不起作用?

android - 禁用特定 fragment 的滑动手势(抽屉导航)

一个 APK 中的 Android 内容提供程序和应用程序?

php - 为什么我的应用程序 (Android) 和网站 (PHP) 的相同帐户无法获得相同的 Facebook ID?

android - Android 上持久移动连接的最佳实践?

xml - 如何将具有不同命名空间的相同 XML 元素反序列化为结构中的不同元素

java - 使用 xpath 根据另一个属性的值检索属性值