Android 在相对位置上相互重叠的元素
我创建了一个活动,并使用了Android Studio中默认的Android 在相对位置上相互重叠的元素,android,android-layout,textview,android-relativelayout,Android,Android Layout,Textview,Android Relativelayout,我创建了一个活动,并使用了Android Studio中默认的RelativeLayout 我正试图在我的文本视图前放置一个按钮,但我无法这样做 结果如下: 这是我的密码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_pa
RelativeLayout
我正试图在我的文本视图
前放置一个按钮,但我无法这样做
结果如下:
这是我的密码:
<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>
看来我错过了什么。任何帮助都将不胜感激 我想您首先需要一个按钮,然后是一个文本视图。然后你可以这样做:
<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>
在您的按钮中,使两个视图相互对齐。我找到的解决方案是:使用下面的
android:layout\u
而不是android:layout\u alignTop
/android:layou 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"
/>
以下是截图:
…您的解决方案也不起作用
layout\u alignRight
和“layout\u alignTop”将使按钮的顶部和右侧与文本视图对齐,因此它们将彼此重叠,可能重复
<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"
/>