ScrollView 内容向上移动
ScrollView contents move up
我正在使用 ScrollView
。虽然之前用过,但是突然遇到了问题。在某些时候内容开始上升。
在我的 AndroidStudio .xml 文件中一切正常:
但是在我的设备上我得到了这个:
如您所见,"The notes go here" 文本已上移。我按下按钮,一部分被切断了。
我的 .xml 代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout 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"
android:id="@+id/mainLayout">
....
</RelativeLayout>
</ScrollView>
还有我的activity:
public class ProfileInfo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_info);
}
}
[更新]
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout 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"
android:id="@+id/mainLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Profile"
android:id="@+id/ProfHead"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:id="@+id/imageProfile"
android:src="@drawable/profile_icon"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_above="@+id/contactsBtn"
android:layout_below="@+id/ProfHead" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Home Phone a"
android:id="@+id/anchor1"
android:visibility="invisible"
android:layout_alignLeft="@+id/nameHead"
android:layout_alignStart="@+id/nameHead" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name"
android:id="@+id/nameHead"
android:layout_below="@+id/patProfHead"
android:layout_marginTop="200dp"
android:layout_marginLeft="20dp"
android:layout_alignParentLeft="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contacts >"
android:id="@+id/contactsBtn"
android:layout_above="@+id/editName"
android:layout_toRightOf="@id/anchor1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:id="@+id/editName"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="@+id/nameHead"
android:layout_marginLeft="20dp"
android:layout_alignParentLeft="true"
android:text="Name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginRight="20dp"
android:id="@+id/editSurname"
android:layout_below="@+id/nameHead"
android:layout_toRightOf="@id/editName"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Sex"
android:id="@+id/sexHead"
android:layout_marginTop="10dp"
android:layout_below="@id/editName"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/anchor1"
android:text="Sex"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignBottom="@id/sexHead"
android:id="@+id/radioGroup">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Age"
android:id="@+id/ageHead"
android:layout_below="@id/sexHead"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="Age"
android:id="@+id/editAge"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_toRightOf="@id/anchor1"
android:layout_alignBaseline="@id/ageHead"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="DOB"
android:id="@+id/dobHead"
android:layout_below="@id/ageHead"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="DOB"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/editDob"
android:layout_toRightOf="@id/anchor1"
android:layout_alignBaseline="@id/dobHead"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Height"
android:id="@+id/heightHead"
android:layout_below="@id/dobHead"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Height"
android:id="@+id/editHeight"
android:layout_toRightOf="@id/anchor1"
android:layout_alignBaseline="@id/heightHead"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Weight"
android:id="@+id/weightHead"
android:layout_below="@id/heightHead"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weight"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/editWeight"
android:layout_toRightOf="@id/anchor1"
android:layout_marginRight="20dp"
android:layout_alignBaseline="@id/weightHead"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Notes"
android:id="@+id/notesHead"
android:layout_below="@id/weightHead"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Template >"
android:id="@+id/notesBtn"
android:layout_alignBaseline="@id/notesHead"
android:layout_toRightOf="@id/anchor1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/editNotes"
android:layout_below="@+id/notesBtn"
android:layout_marginLeft="20dp"
android:text="The notes go here"
android:layout_marginRight="20dp"
android:layout_alignParentLeft="true" />
</RelativeLayout>
</ScrollView>
问题 1:您在 ScrollView 内的视图不应具有与父级匹配的高度。
这违背了 ScrollView 的目的。视图有它自己的大小并且滚动包含它(通常内部视图更大并且 ScrollView 滚动它)。
尝试使用换行内容。
问题 2:您的 ScrollView 的高度="wrap_content"。这是一个错误,ScrollView 没有根据内容调整它的高度。尝试给它明确的高度或给它一个高度="match_parent".
问题 3:您在其他内容之上看到的文字是使用 RelativeLayout 的结果。在 RelativeLayout 中,视图相对于父级的 top/bottom/left/right 彼此重叠。
如果您不希望它们重叠,请使用 LinearLayout 而不是 RelativeLayout 或在 RelativeLayout 中使用具有多个子项的 LinearLayout。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mainLayout">
</LinearLayout>
</ScrollView>
首先使用垂直线性布局。然后在每一行中使用水平线性布局。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout 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="wrap_content"
android:orientation="vertical"
android:id="@+id/mainLayout">
<LinearLayout 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="wrap_content"
android:orientation="horizontal"
android:id="@+id/mainLayout">
//here put your code for each line.
</LinearLayout>
//then again use a linear layout horizontal for next line
.
.
.
.
</LinearLayout>
</ScrollView>
这几乎可以是你想要的。您可以对其进行一些自定义以实现您想要的效果。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Profile"
android:id="@+id/ProfHead"
android:layout_marginTop="20dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:id="@+id/imageProfile"
android:src="@drawable/profile_icon"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contacts >"
android:id="@+id/contactsBtn"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Home Phone a"
android:id="@+id/anchor1"
android:visibility="invisible"
android:layout_alignLeft="@+id/nameHead"
android:layout_alignStart="@+id/nameHead" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name"
android:id="@+id/nameHead"
android:layout_marginLeft="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:id="@+id/editName"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="20dp"
android:text="Name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginRight="20dp"
android:id="@+id/editSurname"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Sex"
android:layout_marginRight="20dp"
android:id="@+id/sexHead"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sex"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/radioGroup">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Age"
android:id="@+id/ageHead"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:text="Age"
android:id="@+id/editAge"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="DOB"
android:id="@+id/dobHead"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:text="DOB"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/editDob"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Height"
android:id="@+id/heightHead"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Height"
android:id="@+id/editHeight"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Weight"
android:id="@+id/weightHead"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weight"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/editWeight"
android:layout_marginTop="10dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Notes"
android:id="@+id/notesHead"
android:layout_marginLeft="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Template >"
android:layout_marginLeft="20dp"
android:id="@+id/notesBtn"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/editNotes"
android:layout_marginLeft="20dp"
android:text="The notes go here"
android:layout_marginRight="20dp"/>
</LinearLayout>
</ScrollView>
我正在使用 ScrollView
。虽然之前用过,但是突然遇到了问题。在某些时候内容开始上升。
在我的 AndroidStudio .xml 文件中一切正常:
但是在我的设备上我得到了这个:
如您所见,"The notes go here" 文本已上移。我按下按钮,一部分被切断了。
我的 .xml 代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout 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"
android:id="@+id/mainLayout">
....
</RelativeLayout>
</ScrollView>
还有我的activity:
public class ProfileInfo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_info);
}
}
[更新]
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout 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"
android:id="@+id/mainLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Profile"
android:id="@+id/ProfHead"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:id="@+id/imageProfile"
android:src="@drawable/profile_icon"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_above="@+id/contactsBtn"
android:layout_below="@+id/ProfHead" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Home Phone a"
android:id="@+id/anchor1"
android:visibility="invisible"
android:layout_alignLeft="@+id/nameHead"
android:layout_alignStart="@+id/nameHead" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name"
android:id="@+id/nameHead"
android:layout_below="@+id/patProfHead"
android:layout_marginTop="200dp"
android:layout_marginLeft="20dp"
android:layout_alignParentLeft="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contacts >"
android:id="@+id/contactsBtn"
android:layout_above="@+id/editName"
android:layout_toRightOf="@id/anchor1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:id="@+id/editName"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="@+id/nameHead"
android:layout_marginLeft="20dp"
android:layout_alignParentLeft="true"
android:text="Name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginRight="20dp"
android:id="@+id/editSurname"
android:layout_below="@+id/nameHead"
android:layout_toRightOf="@id/editName"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Sex"
android:id="@+id/sexHead"
android:layout_marginTop="10dp"
android:layout_below="@id/editName"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/anchor1"
android:text="Sex"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignBottom="@id/sexHead"
android:id="@+id/radioGroup">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Age"
android:id="@+id/ageHead"
android:layout_below="@id/sexHead"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="Age"
android:id="@+id/editAge"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_toRightOf="@id/anchor1"
android:layout_alignBaseline="@id/ageHead"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="DOB"
android:id="@+id/dobHead"
android:layout_below="@id/ageHead"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="DOB"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/editDob"
android:layout_toRightOf="@id/anchor1"
android:layout_alignBaseline="@id/dobHead"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Height"
android:id="@+id/heightHead"
android:layout_below="@id/dobHead"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Height"
android:id="@+id/editHeight"
android:layout_toRightOf="@id/anchor1"
android:layout_alignBaseline="@id/heightHead"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Weight"
android:id="@+id/weightHead"
android:layout_below="@id/heightHead"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weight"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/editWeight"
android:layout_toRightOf="@id/anchor1"
android:layout_marginRight="20dp"
android:layout_alignBaseline="@id/weightHead"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Notes"
android:id="@+id/notesHead"
android:layout_below="@id/weightHead"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Template >"
android:id="@+id/notesBtn"
android:layout_alignBaseline="@id/notesHead"
android:layout_toRightOf="@id/anchor1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/editNotes"
android:layout_below="@+id/notesBtn"
android:layout_marginLeft="20dp"
android:text="The notes go here"
android:layout_marginRight="20dp"
android:layout_alignParentLeft="true" />
</RelativeLayout>
</ScrollView>
问题 1:您在 ScrollView 内的视图不应具有与父级匹配的高度。 这违背了 ScrollView 的目的。视图有它自己的大小并且滚动包含它(通常内部视图更大并且 ScrollView 滚动它)。 尝试使用换行内容。
问题 2:您的 ScrollView 的高度="wrap_content"。这是一个错误,ScrollView 没有根据内容调整它的高度。尝试给它明确的高度或给它一个高度="match_parent".
问题 3:您在其他内容之上看到的文字是使用 RelativeLayout 的结果。在 RelativeLayout 中,视图相对于父级的 top/bottom/left/right 彼此重叠。 如果您不希望它们重叠,请使用 LinearLayout 而不是 RelativeLayout 或在 RelativeLayout 中使用具有多个子项的 LinearLayout。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mainLayout">
</LinearLayout>
</ScrollView>
首先使用垂直线性布局。然后在每一行中使用水平线性布局。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout 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="wrap_content"
android:orientation="vertical"
android:id="@+id/mainLayout">
<LinearLayout 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="wrap_content"
android:orientation="horizontal"
android:id="@+id/mainLayout">
//here put your code for each line.
</LinearLayout>
//then again use a linear layout horizontal for next line
.
.
.
.
</LinearLayout>
</ScrollView>
这几乎可以是你想要的。您可以对其进行一些自定义以实现您想要的效果。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Profile"
android:id="@+id/ProfHead"
android:layout_marginTop="20dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:id="@+id/imageProfile"
android:src="@drawable/profile_icon"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contacts >"
android:id="@+id/contactsBtn"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Home Phone a"
android:id="@+id/anchor1"
android:visibility="invisible"
android:layout_alignLeft="@+id/nameHead"
android:layout_alignStart="@+id/nameHead" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name"
android:id="@+id/nameHead"
android:layout_marginLeft="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:id="@+id/editName"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="20dp"
android:text="Name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginRight="20dp"
android:id="@+id/editSurname"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Sex"
android:layout_marginRight="20dp"
android:id="@+id/sexHead"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sex"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/radioGroup">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Age"
android:id="@+id/ageHead"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:text="Age"
android:id="@+id/editAge"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="DOB"
android:id="@+id/dobHead"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:text="DOB"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/editDob"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Height"
android:id="@+id/heightHead"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Height"
android:id="@+id/editHeight"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Weight"
android:id="@+id/weightHead"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weight"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/editWeight"
android:layout_marginTop="10dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Notes"
android:id="@+id/notesHead"
android:layout_marginLeft="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Template >"
android:layout_marginLeft="20dp"
android:id="@+id/notesBtn"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/editNotes"
android:layout_marginLeft="20dp"
android:text="The notes go here"
android:layout_marginRight="20dp"/>
</LinearLayout>
</ScrollView>