如何使用滚动视图?是否有示例代码?

问题描述:

我在做Android应用程序。主要活动是一个列表视图。主要活动的xml文件如下。如何使用滚动视图?是否有示例代码?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:gravity="fill_horizontal" 
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=".MainActivity" > 

<TabHost 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center_horizontal" 
     android:orientation="vertical" > 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 
     </TabWidget> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <LinearLayout 
       android:id="@+id/tab1" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" > 

       <TableLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" > 

        <TableRow 
         android:id="@+id/TableRow01" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_alignParentTop="true" 
         android:layout_centerHorizontal="true" 
         android:layout_gravity="center" 
         android:gravity="fill" 
         android:visibility="gone"> 

         <Button 
          android:id="@+id/Button01" 
          android:layout_width="48dp" 
          android:layout_height="48dp" 
          android:background="@drawable/ic_action_search" 
          android:onClick="search" /> 

         <EditText 
          android:id="@+id/EditText01" 
          android:layout_width="match_parent" 
          android:layout_height="48dp" 
          android:layout_gravity="fill_horizontal" 
          android:ems="10" 
          android:hint="Employer?date?program?" 
          /> 
        </TableRow> 
       </TableLayout> 

       <ListView 
        android:id="@+id/taboneviewlist" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" > 
       </ListView> 
      </LinearLayout> 
</TabHost> 

当我们单击列表视图中的每个项目,出现data_activity。列表视图中每个项目(data_activity)的xml文件如下所示。

<TableLayout xmlns:tools="http://schemas.android.com/tools" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/TableLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 
<TextView 
    android:id="@+id/employer" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:gravity="center" 
    android:textSize="24sp" 
    android:textStyle="normal|bold" /> 

<View 
    android:id="@+id/employer_line" 
    android:layout_height="4sp" 
    android:layout_width="fill_parent" 
    android:background="#FF909090" 
    android:visibility="gone"/> 
<!-- Table: Date --> 
<TableRow 
    android:id="@+id/tableRow2" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginLeft="2dp" 
    android:layout_marginRight="2dp"> 

    <TextView 
     android:id="@+id/date_title" 
     android:layout_width="100dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.3" 
     android:gravity="left" 
     android:text="Time" 
     android:textSize="20sp" 
     android:textStyle="normal|bold" /> 

    <TextView 
     android:id="@+id/date" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.7" 
     android:gravity="right" 
     android:textColor="#999999" 
     android:textSize="20sp" /> 
</TableRow> </TableLayout> 

现在data_activity不能显示所有的信息。问题是如何使data_activity滚动?有没有任何示例代码?

把你的顶层视图为ScrollView

<ScrollView xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <TableLayout 
     android:id="@+id/TableLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
     ... 
    </TableLayout> 
</ScrollView> 
+0

今天早晨我试过这个方法。但是当我点击列表视图中的每个项目时,data_activity没有出现。 – BAE 2014-09-10 22:42:35

+0

@程城沛究竟是怎么没有出现?它打印什么记录? – 2014-09-10 23:19:31

+0

看来以下功能不起作用。 “某事”不会显示在logcat上。代码:''DataListView =(ListView)findViewById(R.id.taboneviewlist); DataListView.setFastScrollEnabled(true); mListAdapter = new ListAdapterData(this,data1); DataListView.setAdapter(mListAdapter); DataListView.setOnItemClickListener(新OnItemClickListener(){ 公共无效onItemClick(适配器视图>一,视图V,INT位置,长的id){// Log.v( “标签”, “东西”) } }); '' – BAE 2014-09-11 20:01:48