统筹布局和相对布局问题 [英] Coordinator Layout and Relative Layout issue
问题描述
当您创建在Android Studio中空白的活动,这是给定的布局:
<?XML版本=1.0编码=UTF-8&GT?;
< android.support.design.widget.CoordinatorLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:fitsSystemWindows =真
工具:上下文=com.example.MainActivity> < android.support.design.widget.AppBarLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:主题=@风格/ AppTheme.AppBarOverlay> < android.support.v7.widget.Toolbar
机器人:ID =@ + ID /工具栏
机器人:layout_width =match_parent
机器人:layout_height =?ATTR / actionBarSize
机器人:ATTR / colorPrimary背景=
应用:popupTheme =@风格/ AppTheme.PopupOverlay/> < /android.support.design.widget.AppBarLayout> <包括布局=@布局/ content_main/> < android.support.design.widget.FloatingActionButton
机器人:ID =@ + ID / FAB
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =底部|结束
机器人:layout_margin =@扪/ fab_margin
机器人:SRC =@机器人:可绘制/ ic_dialog_email/>< /android.support.design.widget.CoordinatorLayout>
和相对布局( @布局/ content_main
)熄灭屏幕:
如果您设置在相对布局的一个按钮,使用 layout_alignParentBottom
按钮有时会走出画面。 (它不发生的事情,我不知道为什么)
@布局/ content_main
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
应用:layout_behavior =@字符串/ appbar_scrolling_view_behavior
工具:上下文=com.example.MainActivity
工具:舒=@布局/ activity_main> <按钮
机器人:ID =@ + ID /按钮
机器人:layout_alignParentBottom =真
机器人:layout_width =match_parent
机器人:layout_height =70dp
机器人:背景=@色/绿
机器人:文字颜色=@色/白
机器人:文本=TEXT
机器人:可聚焦=真
机器人:focusableInTouchMode =真
/>< / RelativeLayout的>
添加填充底部相对布局将无法工作。 (当此按键关闭屏幕它只会工作)
我需要的按钮是在总是在屏幕的底部。谁能告诉我如何实现这一目标?
这样的:
请尝试以下code ,这将保持在结束该按钮而不去出的观点:
<?XML版本=1.0编码=UTF-8&GT?;
< android.support.design.widget.CoordinatorLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:fitsSystemWindows =真
工具:上下文=com.example.android.myapplication.MainActivity> < android.support.design.widget.AppBarLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:主题=@风格/ AppTheme.AppBarOverlay> < android.support.v7.widget.Toolbar
机器人:ID =@ + ID /工具栏
机器人:layout_width =match_parent
机器人:layout_height =?ATTR / actionBarSize
机器人:ATTR / colorPrimary背景=
应用:popupTheme =@风格/ AppTheme.PopupOverlay/> < /android.support.design.widget.AppBarLayout>
< android.support.design.widget.FloatingActionButton
机器人:ID =@ + ID / FAB
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =底部|结束
机器人:layout_margin =@扪/ fab_margin
机器人:SRC =@机器人:可绘制/ ic_dialog_email/> <按钮
机器人:ID =@ + ID / BTN。
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =底/>< /android.support.design.widget.CoordinatorLayout>
和相对布局(@布局/ content_main)熄灭屏幕:
块引用>编辑:我想这是因为你设置它们
match_parent
。尝试是这样的:<?XML版本=1.0编码=UTF-8&GT?;
< android.support.design.widget.CoordinatorLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:fitsSystemWindows =真
工具:上下文=com.example.MainActivity> < android.support.design.widget.AppBarLayout
机器人:ID =@ + ID / app_bar
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:背景=@机器人:彩色/透明
机器人:fitsSystemWindows =真
机器人:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar> < android.support.design.widget.CollapsingToolbarLayout
机器人:ID =@ + ID / collapsing_toolbar
机器人:layout_width =match_parent
机器人:layout_height =300dp
机器人:背景=@彩色/ colorPrimary
机器人:fitsSystemWindows =真
应用:contentScrim =?ATTR / colorPrimary
应用:layout_scrollFlags =滚动| exitUntilCollapsed> < android.support.v7.widget.Toolbar
机器人:ID =@ + ID /工具栏
机器人:layout_width =match_parent
机器人:layout_height =?ATTR / actionBarSize
机器人:ATTR / colorPrimary背景=
应用:popupTheme =@风格/ AppTheme.PopupOverlay/> < /android.support.design.widget.CollapsingToolbarLayout> < /android.support.design.widget.AppBarLayout> < android.support.design.widget.FloatingActionButton
机器人:ID =@ + ID / FAB
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =底部|结束
机器人:layout_margin =@扪/ fab_margin
机器人:SRC =@机器人:可绘制/ ic_dialog_email/> < RelativeLayout的
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT> <按钮
机器人:ID =@ + ID /按钮
机器人:layout_width =match_parent
机器人:layout_height =70dp
机器人:layout_alignParentBottom =真
机器人:背景=@彩色/ colorAccent
机器人:可聚焦=真
机器人:focusableInTouchMode =真
机器人:文本=TEXT
机器人:文字颜色=@机器人:彩色/白/> < / RelativeLayout的>< /android.support.design.widget.CoordinatorLayout>When you create a blank Activity in android studio, this is the given layout:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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" android:fitsSystemWindows="true" tools:context="com.example.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout>
And the Relative layout (
@layout/content_main
) goes out of screen:If you set a button in your relative layout, with
layout_alignParentBottom
the button sometimes will go out of screen. (It doesn't happen all the time, I have no idea why)@layout/content_main
<?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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.MainActivity" tools:showIn="@layout/activity_main"> <Button android:id="@+id/button" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="70dp" android:background="@color/green" android:textColor="@color/white" android:text="text" android:focusable = "true" android:focusableInTouchMode = "true" /> </RelativeLayout>
Adding Padding bottom to the relative layout won't work. (As it'll only work when the button is off the screen)
I need the button to be at the bottom of the screen always. Can someone tell me how to achieve this?
Like this:
解决方案Try the following code, this will keep that button at the end without going to out of the view:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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" android:fitsSystemWindows="true" tools:context="com.example.android.myapplication.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" /> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" /> </android.support.design.widget.CoordinatorLayout>
And the Relative layout (@layout/content_main) goes out of screen:
Edit: i think that's because you've set them
match_parent
.try something like this:<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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" android:fitsSystemWindows="true" tools:context="com.example.MainActivity"> <android.support.design.widget.AppBarLayout android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/transparent" android:fitsSystemWindows="true" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" android:layout_width="match_parent" android:layout_height="300dp" android:background="@color/colorPrimary" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="70dp" android:layout_alignParentBottom="true" android:background="@color/colorAccent" android:focusable="true" android:focusableInTouchMode="true" android:text="text" android:textColor="@android:color/white" /> </RelativeLayout> </android.support.design.widget.CoordinatorLayout>
这篇关于统筹布局和相对布局问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!