如何将滚动视图添加到Android布局中
我有以下布局,但我想让它垂直滚动,如果内容太多的显示。ImageView“imagePhoto”是使用设备摄像头拍摄的图像。如果照片是纵向拍摄的,那么布局需要能够滚动。我尝试在LinearLayout之前添加一个ScrollView,如下所示:如何将滚动视图添加到Android布局中,android,android-layout,android-widget,xamarin.android,Android,Android Layout,Android Widget,Xamarin.android,我有以下布局,但我想让它垂直滚动,如果内容太多的显示。ImageView“imagePhoto”是使用设备摄像头拍摄的图像。如果照片是纵向拍摄的,那么布局需要能够滚动。我尝试在LinearLayout之前添加一个ScrollView,如下所示: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:la
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
但它似乎不起作用
有什么建议吗
以下是布局xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widgetUploadPhoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/topLevelMenuBackground"
android:orientation="horizontal">
<TextView
android:id="@+id/tvHeaderText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text=""
android:textColor="#ffffffff"
android:textSize="20sp"
android:shadowColor="#7F000000"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="2"
android:layout_centerVertical="true" />
</RelativeLayout>
<TextView
android:id="@+id/tvYourCurrentPhoto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Your current photo:"
android:background="#ffffffff"
android:textColor="#ff626262"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:textSize="@dimen/text_size_medium"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
android:visibility="gone" />
<ImageView
android:id="@+id/imagePhoto"
android:src="@drawable/OrangeAccessoryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:background="#ffffffff"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:paddingTop="15dp"
android:paddingRight="15dp"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:visibility="gone" />
<TextView
android:id="@+id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Select an option below to change your photo."
android:background="#ffffffff"
android:textColor="#ff626262"
android:paddingTop="15dp"
android:paddingRight="15dp"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:textSize="@dimen/text_size_medium" />
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:focusable="false"
android:layout_weight="1" />
</LinearLayout>
谢谢你的帮助/建议
更新的XML:
注意:这是可行的,但不是预期的结果。我只想让RelativeView中的内容滚动。第一个TexView是一个标题,我希望它保持静止,下面的视图可以滚动。基本上,所有内容都应该滚动,但相对vie部分除外
<?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="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widgetUploadPhoto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffffff"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/topLevelMenuBackground"
android:orientation="horizontal">
<TextView
android:id="@+id/tvHeaderText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text=""
android:textColor="#ffffffff"
android:textSize="20sp"
android:shadowColor="#7F000000"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="2"
android:layout_centerVertical="true" />
</RelativeLayout>
<TextView
android:id="@+id/tvYourCurrentPhoto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Your current photo:"
android:background="#ffffffff"
android:textColor="#ff626262"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:textSize="@dimen/text_size_medium"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
android:visibility="gone" />
<ImageView
android:id="@+id/imagePhoto"
android:src="@drawable/OrangeAccessoryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:background="#ffffffff"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:paddingTop="15dp"
android:paddingRight="15dp"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:visibility="gone" />
<TextView
android:id="@+id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Select an option below to change your photo."
android:background="#ffffffff"
android:textColor="#ff626262"
android:paddingTop="15dp"
android:paddingRight="15dp"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:textSize="@dimen/text_size_medium" />
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:focusable="false"
android:layout_weight="1" />
</LinearLayout>
</ScrollView>
这将有一个标题
TextView
,该标题不会在顶部滚动,然后是垂直滚动的下方所需的内容。请特别注意滚动视图的属性,android:height=“0dp”
和android:weight=“1”
的组合将使其在绘制顶部文本视图后展开以填充所有剩余空间
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical>
<TextView android:id="@+id/tvHeaderText"
android:background="@drawable/topLevelMenuBackground"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColor="#ffffffff"
android:textSize="20sp"
android:shadowColor="#7F000000"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="2" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- Everything you want to scroll goes here -->
</LinearLayout>
</ScrollView>
</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 xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widgetUploadPhoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/topLevelMenuBackground"
android:orientation="horizontal">
<TextView
android:id="@+id/tvHeaderText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text=""
android:textColor="#ffffffff"
android:textSize="20sp"
android:shadowColor="#7F000000"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="2"
android:layout_centerVertical="true" />
</RelativeLayout>
<TextView
android:id="@+id/tvYourCurrentPhoto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Your current photo:"
android:background="#ffffffff"
android:textColor="#ff626262"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:textSize="@dimen/text_size_medium"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
android:visibility="gone" />
<ImageView
android:id="@+id/imagePhoto"
android:src="@drawable/OrangeAccessoryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:background="#ffffffff"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:paddingTop="15dp"
android:paddingRight="15dp"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:visibility="gone" />
<TextView
android:id="@+id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Select an option below to change your photo."
android:background="#ffffffff"
android:textColor="#ff626262"
android:paddingTop="15dp"
android:paddingRight="15dp"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:textSize="@dimen/text_size_medium" />
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:focusable="false"
android:layout_weight="1" />
结构如下所示:
<LinearLayout>
<RelativeLayout>
<TextView android:id="@+id/tvHeaderText" />
</RelativeLayout>
<ScrollView>
<LinearLayout android:id="@+id/widgetUploadPhoto" >
<TextView android:id="@+id/tvYourCurrentPhoto"/>
<ImageView android:id="@+id/imagePhoto" />
<TextView android:id="@+id/empty"/>
<ListView android:id="@+id/listView"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
首先,将LinearLayout
作为根。然后将RelativeLayout
移动到根LinearLayout
下,使其始终位于顶部。接下来,在RelativeLayout
之后放置一个ScrollView
,并将另一个LinearLayout
作为scorlView
的子级
另外,我不认为将列表视图
放在滚动视图
中是个好主意。请参阅。您正在手机或平板电脑上测试哪种设备?请注意,在手机上工作的东西可能在平板电脑上不工作,因此对于手机和sw600dp land,您必须进行测试和自定义。我已经为图表实现了Scrollview,我正在这里分享我的工作代码
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/back"
android:scrollbars="vertical"
android:textSize="20sp">
<!--Month Combo Box-->
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Month"
android:textColor="#0c0b0b"
android:textSize="20sp" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0c0b0b"
android:textSize="20sp" />
<Spinner
android:id="@+id/spinneryr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0c0b0b"
android:textSize="20sp" />
</TableRow>
<!--Field1 Combo Box-->
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ERY"
android:textColor="#0c0b0b"
android:textSize="20sp" />
<EditText
android:id="@+id/ERYS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:gravity="center"
android:inputType="numberDecimal" />
</TableRow>
<!--Field9 Combo Box-->
<TableRow
android:id="@+id/tableRow10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/PCOUNT"
android:textColor="#0c0b0b"
android:textSize="20sp" />
<EditText
android:id="@+id/PLS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:gravity="center"
android:inputType="numberDecimal" />
</TableRow>
<!--Field10 Combo Box-->
<TableRow
android:id="@+id/tableRow11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/NES"
android:textColor="#0c0b0b"
android:textSize="20sp" />
<EditText
android:id="@+id/NESSS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:gravity="center"
android:inputType="numberDecimal" />
</TableRow>
<!--Field11 Combo Box-->
<TableRow
android:id="@+id/tableRow12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/EOS"
android:textColor="#0c0b0b"
android:textSize="20sp" />
<EditText
android:id="@+id/EOSS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:gravity="center"
android:inputType="numberDecimal" />
</TableRow>
<!--Field12 Combo Box-->
<!--Removed android:layout_gravity="center"-->
<TableRow
android:id="@+id/tableRow13"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/LYMS"
android:textColor="#0c0b0b"
android:textSize="20sp" />
<EditText
android:id="@+id/LYMSS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:gravity="center"
android:inputType="numberDecimal" />
</TableRow>
<!-- Button :Start here-->
<TableRow
android:id="@+id/tableRow14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20sp">
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add" />
<Button
android:id="@+id/btnViewAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/view_all" />
</TableRow>
<TextView
android:id="@+id/tvValues"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp" />
</TableLayout>
</ScrollView>
好的,是的,这是可行的,但没有产生我想要的重用。RelativeLayout部分中的第一个TextView是视图的标题。我不想让它滚动。我已经编辑了我的文章,并添加了更新的XML,这是可行的,但整个布局会滚动。我怎样才能在RelativeLayout滚动之后制作这些东西呢?我编辑了我的答案来做你想做的。您的RelativeLayout
没有必要,因此我将其删除。我注意到你有一个列表视图
,这就是你想要滚动的全部内容吗?不,我想要滚动相对位置下方的所有内容。因此,从文本视图-@+id/tvYourCurrentPhoto开始。现在我确实移动了ScrollView,但是ListView中有两个项目,第二个项目被切断。我认为这与列表视图中的布局高度有关。这就是我所做的,将您想要滚动的所有内容放在我发布的代码中注释的位置。RelativeLayout
不是必需的,只需将其中的TextView
设置为相同的背景,然后删除RelativeLayout
。Ralgha所说的确实有效,但没有产生我所期望的结果。请查看我的评论和更新的帖子。请删除相关布局,然后重试。我不确定,但我认为它不适合相对布局。我也遇到了同样的问题。拉凯什,拉尔加提供的解决方案很好,但这并不是我想要的。我只想在相对布局下的东西滚动。这就是我现在正在努力解决的问题。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/back"
android:scrollbars="vertical"
android:textSize="20sp">
<!--Month Combo Box-->
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Month"
android:textColor="#0c0b0b"
android:textSize="20sp" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0c0b0b"
android:textSize="20sp" />
<Spinner
android:id="@+id/spinneryr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0c0b0b"
android:textSize="20sp" />
</TableRow>
<!--Field1 Combo Box-->
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ERY"
android:textColor="#0c0b0b"
android:textSize="20sp" />
<EditText
android:id="@+id/ERYS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:gravity="center"
android:inputType="numberDecimal" />
</TableRow>
<!--Field9 Combo Box-->
<TableRow
android:id="@+id/tableRow10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/PCOUNT"
android:textColor="#0c0b0b"
android:textSize="20sp" />
<EditText
android:id="@+id/PLS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:gravity="center"
android:inputType="numberDecimal" />
</TableRow>
<!--Field10 Combo Box-->
<TableRow
android:id="@+id/tableRow11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/NES"
android:textColor="#0c0b0b"
android:textSize="20sp" />
<EditText
android:id="@+id/NESSS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:gravity="center"
android:inputType="numberDecimal" />
</TableRow>
<!--Field11 Combo Box-->
<TableRow
android:id="@+id/tableRow12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/EOS"
android:textColor="#0c0b0b"
android:textSize="20sp" />
<EditText
android:id="@+id/EOSS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:gravity="center"
android:inputType="numberDecimal" />
</TableRow>
<!--Field12 Combo Box-->
<!--Removed android:layout_gravity="center"-->
<TableRow
android:id="@+id/tableRow13"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/LYMS"
android:textColor="#0c0b0b"
android:textSize="20sp" />
<EditText
android:id="@+id/LYMSS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:gravity="center"
android:inputType="numberDecimal" />
</TableRow>
<!-- Button :Start here-->
<TableRow
android:id="@+id/tableRow14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20sp">
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add" />
<Button
android:id="@+id/btnViewAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/view_all" />
</TableRow>
<TextView
android:id="@+id/tvValues"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp" />
</TableLayout>
</ScrollView>