Android 如何在两个布局之间定位按钮
我试图在两个布局之间放置一个按钮 另外,如果我能帮上忙的话,我不想在处理不同的屏幕尺寸时使用边距。 (在本图中,我试图将绿色按钮放置在两个布局之间)Android 如何在两个布局之间定位按钮,android,android-layout,android-button,Android,Android Layout,Android Button,我试图在两个布局之间放置一个按钮 另外,如果我能帮上忙的话,我不想在处理不同的屏幕尺寸时使用边距。 (在本图中,我试图将绿色按钮放置在两个布局之间) 我认为您应该尝试使用一个RelativeLayout,在其中放置两个布局和按钮。 但是如果你想把按钮放在两个布局的顶部,你必须把它放在你的RelativeLayout中,因为z顺序,然后使用XML属性android:centerInParent 因此,您的布局如下所示: <RelativeLayout ... android:lay
我认为您应该尝试使用一个
RelativeLayout
,在其中放置两个布局和按钮。
但是如果你想把按钮放在两个布局的顶部,你必须把它放在你的RelativeLayout
中,因为z顺序,然后使用XML属性android:centerInParent
因此,您的布局如下所示:
<RelativeLayout ...
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout ...
android:id="@+id/myLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" > <!-- Or wherever you want to position this layout -->
...
</LinearLayout>
<LinearLayout ...
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/myLayout1" > <!-- Or wherever you want to position this layout -->
...
</LinearLayout>
<Button ...
android:layout_centerInParent="true" />
</RelativeLayout>
...
...
这种UI需要多层重叠。
所以FrameLayout是这里的英雄。
为了给出我们希望实现的基本想法,我们可以绘制屏幕草图并决定位置
FL是一个框架布局的主容器。
你需要做一个固定高度的按钮。假设BL的高度为BL-dp。
只需为线性布局LL提供长度为bl/2 dp的边缘底部。
其中LL是包含配置文件图像和作品的容器 布局文件如下所示
<LinearLayout 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="@android:color/white"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/ConcernedPortionofScreen"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4"
android:orientation="vertical">
<!-- Parent FrameLayout 'FL' -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This is Layout 'LL'
This is where you will place your image & the nice bg
-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="25dp"
android:background="#b2ebf2" />
<!-- BL = 50dp -->
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_gravity="bottom|center_horizontal"
android:background="#558b2f"
android:text="@android:string/ok"
android:textSize="18sp"
android:textColor="@android:color/white" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/RestofScreen"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.6"
android:orientation="vertical" />
</LinearLayout>
输出将如下所示
<LinearLayout 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="@android:color/white"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/ConcernedPortionofScreen"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4"
android:orientation="vertical">
<!-- Parent FrameLayout 'FL' -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This is Layout 'LL'
This is where you will place your image & the nice bg
-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="25dp"
android:background="#b2ebf2" />
<!-- BL = 50dp -->
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_gravity="bottom|center_horizontal"
android:background="#558b2f"
android:text="@android:string/ok"
android:textSize="18sp"
android:textColor="@android:color/white" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/RestofScreen"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.6"
android:orientation="vertical" />
</LinearLayout>
您可以通过使用协调器布局轻松实现这一点。我们必须逐个指定多个布局,并且两个布局都必须是线性布局。现在我们必须在按钮内部使用布局锚属性
<android.support.design.widget.CoordinatorLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#CDDC39"
android:layout_weight="50"/>
<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="50"
android:background="#C6FF00"
/>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="200dp"
android:layout_height="wrap_content"
app:layout_anchor="@id/LinearLayout1"
app:backgroundTint="#2196F3"
app:elevation="7dp"
android:text:"click Me"
android:layout_margin="20dp"
android:src="@android:drawable/ic_dialog_email"
app:layout_anchorGravity="bottom|center" />
</android.support.design.widget.CoordinatorLayout>
您需要将两个布局和按钮嵌套在一个相对的布局中。EY bro,您解决了这个问题吗?我还没有解决它!我确实有一些人告诉我尝试框架布局,但我还没有时间尝试。我想看看Android L对它的支持。Android:layout\u centerInParent=“true”只在布局中居中,不允许在层之间居中。好的,对不起,我没有很好地理解你的问题。你可以试着把你的按钮
放在你的第一个布局下面,然后设置一个负的android:layout\u marginTop
。然后将第二个布局放置在第一个布局下方。(但是,如果你想让你的按钮位于两者之上,你必须把它放在你的RelativeLayout
)我不确定有没有不使用边距的方法。但是您可以在values xxx
文件夹中为边距设置不同的值。非常感谢。。。经过轻微的修改,它对我的用例有效:)嘿,伙计们,我在使用这个时遇到了问题,我在滚动条中使用了这个代码,现在“屏幕的其余部分”在我的上一个项目对象中遇到了问题,它没有显示出来。有什么解决办法吗?