Android 将我的RelativeLayout放在ScrollView中会将布局中的所有项目放错位置
非常感谢您的帮助和关注 在将RelativeLayout放入ScrollView之前,我的活动是这样的: 我仍然需要处理它,但是按钮、编辑文本和视图都在它们的位置上Android 将我的RelativeLayout放在ScrollView中会将布局中的所有项目放错位置,android,android-layout,scrollview,android-relativelayout,Android,Android Layout,Scrollview,Android Relativelayout,非常感谢您的帮助和关注 在将RelativeLayout放入ScrollView之前,我的活动是这样的: 我仍然需要处理它,但是按钮、编辑文本和视图都在它们的位置上 现在,如果我把RelativeLayout放在一个ScrollView中,所有项目的位置都会混乱 这是(不期望的)结果 请问我该怎么办 以下是我的XML: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=
现在,如果我把RelativeLayout放在一个ScrollView中,所有项目的位置都会混乱 这是(不期望的)结果 请问我该怎么办 以下是我的XML:
<ScrollView 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bgland"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MenuActivity" >
<View
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_centerInParent="true"
android:layout_margin="20dip"
android:background="@android:color/darker_gray" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"
android:src="@drawable/white_bg" />
<EditText
android:id="@+id/bikenumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/getbikebutton"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:ems="10"
android:hint="@string/hint_getbike"
android:inputType="number"
android:singleLine="true" >
<requestFocus />
</EditText>
<Button
android:id="@+id/getbikebutton"
style="@style/OrangeButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/view1"
android:layout_alignLeft="@+id/view1"
android:layout_alignRight="@+id/view1"
android:layout_marginBottom="10dip"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:text="@string/menu_getbikebuttontext" />
<Button
android:id="@+id/buttonGoToMyLoc"
style="@style/OrangeButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/view1"
android:layout_alignRight="@+id/view1"
android:layout_below="@+id/view1"
android:layout_marginBottom="10dip"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:text="@string/menu_text_near_me" />
<Button
android:id="@+id/buttonGoToThisLoc"
style="@style/OrangeButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/view1"
android:layout_alignRight="@+id/view1"
android:layout_below="@+id/buttonGoToMyLoc"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:layout_marginTop="10dip"
android:text="@string/menu_text_address" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dip"
android:text="@string/menu_title"
android:textColor="@android:color/black"
android:textSize="20dip"
android:textStyle="bold" />
<EditText
android:id="@+id/locAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_alignRight="@+id/imageView1"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="10dip"
android:ems="10"
android:hint="@string/menu_hint"
android:inputType="text"
android:singleLine="true"
android:visibility="gone" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/locAddressCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_alignRight="@+id/imageView1"
android:layout_below="@+id/locAddress"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:ems="10"
android:hint="@string/menu_hint_city"
android:inputType="text"
android:singleLine="true"
android:visibility="gone" />
</RelativeLayout>
</ScrollView>
在
滚动视图中添加android:fillViewport=“true”
标记。我想这可以解决你的问题。如下代码所示:
<ScrollView 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:fillViewport="true">
如果你想了解更多这方面的信息,你可以检查一下这一点。谢里夫是对的,但是这并不总是足够的。内部布局(ScrollView的唯一子级)不应使用match_parent,因为这本质上意味着它将与ScrollView一样大,但永远不会更大-关键是内部布局将比屏幕上适合的更大,从而比ScrollView更大。根据您的需要,相对布局应该具有包装内容的高度,或者在DP中指定一个固定的大小
把这个和谢里夫的答案结合起来,你就会明白了
这在滚动和布局高度方面对我很有效,但是你的相对布局有点混乱-为什么不使用简单的线性布局,而只是一个接一个地布局按钮/文本呢?这看起来是一种更容易实现相同外观的方法
<ScrollView 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:fillViewport="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MenuActivity" >
... content ...
</RelativeLayout>
... 内容。。。
添加到ScrollView代码中,如下所示:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
tools:context=".MenuActivity" >
<View
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_centerInParent="true"
android:layout_margin="20dip"
android:background="@android:color/darker_gray" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"
android:src="@android:color/white" />
<EditText
android:id="@+id/bikenumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/getbikebutton"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:ems="10"
android:hint="Get Bikes"
android:inputType="number"
android:singleLine="true" >
<requestFocus />
</EditText>
<Button
android:id="@+id/getbikebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/view1"
android:layout_alignLeft="@+id/view1"
android:layout_alignRight="@+id/view1"
android:layout_marginBottom="10dip"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:text="FindBikes" />
<Button
android:id="@+id/buttonGoToMyLoc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/view1"
android:layout_alignRight="@+id/view1"
android:layout_below="@+id/view1"
android:layout_marginBottom="10dip"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:text="NearMe" />
<Button
android:id="@+id/buttonGoToThisLoc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/view1"
android:layout_alignRight="@+id/view1"
android:layout_below="@+id/buttonGoToMyLoc"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:layout_marginTop="10dip"
android:text="Near Address" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dip"
android:text="Title"
android:textColor="@android:color/black"
android:textSize="20dip"
android:textStyle="bold" />
<EditText
android:id="@+id/locAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_alignRight="@+id/imageView1"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:textColor="@android:color/black"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="10dip"
android:ems="10"
android:hint="Addresss"
android:inputType="text"
android:singleLine="true"
android:visibility="gone" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/locAddressCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_alignRight="@+id/imageView1"
android:layout_below="@+id/locAddress"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:ems="10"
android:hint="Address City"
android:inputType="text"
android:singleLine="true"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
ScrollView
附带了LinearLayout
,它只在ScrollView
中使用直接子级。您可以将所有组件放在LinearLayout
内。因此,如果您希望在ScrollView
内有任何布局,则应首先将LinearLayout
放在ScrollView
内,并将所有组件放在LinearLayout
内,哪个id?对不起,谢里夫,我是认真的(“谢谢谢里夫,但它不起作用”),我编辑了评论hi@LisaAnne,我觉得有点不对劲。对于getbikebutton,请使用上面的android:layout_=“@id/view1”,而不是下面的android:layout_=“@id/view1”,并请重新检查您的代码。我认为这应该行得通。我认为你内心的问题。我想你可以自己修。谢谢你Lisa Anne。谢谢你nitzanj,我尝试在RelativeLayout上添加vandroid:fillViewport=“true”`并设置android:layout\u height=“wrap\u content”
,但没有任何变化:-(((我刚刚测试了您的xml,它适用于我的fillViewport和wrap_内容,所以我不确定为什么它不适用于您。当您将相对布局高度设置为1500dp左右时会发生什么情况?正常工作?嗨,nitzanj,我尝试过将android:layout_高度设置为任意大小,但没有任何更改:-(((如果您的XML有效,请您发布它好吗?谢谢!LISAcan您可以给我全屏设计,这样我就可以为您设计整个屏幕了?这很有效!Imran!!!您太棒了!!!请告诉我我不明白有什么区别,我做错了什么scrollview带有linearlayout,并且在scrollview中只有一个布局可以使用。)将所有组件放在该线性布局内。因此,如果您希望在scrollview内有任何布局,则应首先将linearlayout放在scrollview内,并将所有组件放在该线性布局内。这是一个完美的示例,说明了为什么仅代码答案没有帮助。@ImranBetara请编辑您的答案,将解释包含在其中。-1代表code唯一的答案。我看到你的评论中有一个解释。请把它添加到你的答案中,这样这个帖子对未来的访问者会更有用。