在Android Studio中安排xml中的元素
我用了
drawerlayout
在我的XML。 我可以在相对布局中正确对齐,但我正在为此抽屉布局而苦恼。
不能使用我在相对布局中使用的大部分功能。
但我有点设法在我想要的地方对齐元素。 但它给了我另一个问题。
•在Android Studio的XML预览版中,它完美对齐 ,
•在模拟器中(编译时)我看不到 Textview和Button 元素 。
•当我安装在我的手机 ,这些元素都是从底部上面一点 。
你可以在下面看到三张照片,
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将你的按钮和文本视图对齐到屏幕的底部,这正是我想要实现的。
我的问题得到了解决方案。 在元素中使用固定高度可能看起来很完美。 但是,不同屏幕尺寸的手机会有所不同。
因此提供固定高度不是一个好主意。
因此,(如果需要嵌套的LinearLayout)使用的LinearLayout,并设置每个元素的权重 。
如果要将元素对齐为“ 列”,请将方向设置为“ 水平” 。
如果要将元素对齐为行,请将方向设置为“ 垂直” 。
重量将影响元素在定义的布局中占用多少空间。
这里是我的代码,没有使用固定的高度,
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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>
</LinearLayout>
<LinearLayout
android:id="@+id/FirstRL"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/dbListrv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="10">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et"
android:layout_alignParentBottom="true"
android:layout_weight="2" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="New Button"
android:id="@+id/addButton"
android:layout_weight="5" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</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>
最后,我找到了解决我自己的问题。 希望它能帮助别人。
链接地址: http://www.djcxy.com/p/93225.html