Android 元素在相对布局中相互重叠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24748563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Elements overlapping on top of each other in relativeLayout
提问by sachinjain024
I have created an Activity and used RelativeLayout
which comes in by default in Android Studio.
我创建了一个 Activity 并使用了RelativeLayout
它在 Android Studio 中的默认设置。
I am trying to place a button just before my TextView
but I am unable to do that.
我试图在我之前放置一个按钮,TextView
但我无法做到这一点。
Here is the result:
结果如下:
Here is my code:
这是我的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
<TextView
android:id="@+id/heading"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:text="@string/sachin_jain"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignLeft="@+id/heading"
android:layout_alignTop="@+id/heading"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
Looks like I am missing something. Any help would be appreciated!!
看起来我错过了一些东西。任何帮助,将不胜感激!!
回答by sachinjain024
The solution I have found out is: Use android:layout_below
instead of android:layout_alignTop
/ android:layout_alignRight
我发现的解决方案是:使用android:layout_below
代替android:layout_alignTop
/android:layout_alignRight
<TextView
android:id="@+id/heading"
android:text="My First Activity with Relative Layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/button"
android:text="Save"
android:layout_width="300px"
android:layout_height="wrap_content"
android:layout_below="@id/heading"
android:layout_margin="20px"
android:layout_centerHorizontal="true"
/>
Here is the screenshot:
这是屏幕截图:
回答by Illegal Argument
I guess you want a button first and a textview second. Then you can do something like this:
我猜你首先想要一个按钮,然后是一个 textview。然后你可以做这样的事情:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
<TextView
android:id="@+id/heading"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:text="@string/sachin_jain"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignRight="@+id/heading"
android:layout_alignTop="@+id/heading"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
Your problem was that you had:
你的问题是你有:
android:layout_alignLeft="@+id/heading"
android:layout_alignTop="@+id/heading"
in your button that made both the view align top of eachother.
在您的按钮中,使视图彼此对齐。