0

我在我的意见(表headertable)重叠查看相互重叠

private ViewGroup createTable(ViewGroup root) { 
    // TODO Auto-generated method stub 
    TableLayout table = new TableLayout(getActivity()); 
    table.setStretchAllColumns(true); 
    table.setShrinkAllColumns(true); 

    TableLayout headertable = new TableLayout(getActivity()); 
    headertable.setStretchAllColumns(true); 
    headertable.setShrinkAllColumns(true); 

    /* Adding stuff to headertable which contains... */ 
      /* ...table content I DO NOT WANT to scroll*/ 

    root.addView(headertable); 

    for (int i = -2; i <= 100; i++) { 

        if (i > 0) { 
      /*Set up empty views*/ 
       /*...3 empty views will be set*/ 
        } 

     /* Adding stuff to table which contains... */ 
        /* ...table content I WANT to scroll*/ 

    } 

    ScrollView sv = new ScrollView(getActivity()); 
    sv.addView(table); 
    root.addView(sv); 
    return root; 
} 

我基本上打破了一个表到headertable和桌子的活动。我想滚动表,但不是headertable。但是,我的表(它应该在headertable下面)与它重叠。因此,正如你在上面看到的,我添加了空视图(所以它开始于headertable(有三行))之下,但是意识到这是行不通的。只要我向下滚动,空视图就会滑动,并且我的头部会再次受到阻挡。

我的所有观点都是以编程方式制作的。此活动是一项片段活动。 XML文件包含

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#ffffff" > 

</FrameLayout> 

任何帮助,将不胜感激。提前致谢。

+1

我想大问题是:什么样的布局是'root'?它是在最后发布的'FrameLayout'吗?如果是这样,你可能想要交换一个垂直定向的'LinearLayout',它会自动将头部和'ScrollView'定位在彼此之下。 'FrameLayout'不适合托管多个孩子(除非你想*重叠)。另外,什么是'table2',因为我没有看到该变量正在构建的任何地方。 – 2012-04-14 12:35:55

+0

首先,谢谢你的回应。表2应该是可以理解的。我现在更新了它。显然,我忘了在发布之前进行修改。不,它不是一个框架布局,它是一个线性布局中的一个片段。就像我说的那样,它是一个片段活动,因此必须将根(这是一个片段)返回到主FragmentActivity。 – Ryan 2012-04-14 13:10:09

回答

0

我不知道这是否是最好的方式,但在这里就是我如何做到这一点:

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
<FrameLayout 
    android:id="@+id/pane" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
</LinearLayout> 

header_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <TextView 
      android:id="@+id/header" 
      android:layout_width="fill_parent" 
      android:layout_height="30dp" 
      android:layout_alignParentTop="true" /> 
    <ListView 
      android:id="@id/android:list" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_below="@id/header" /> 
    <TextView 
      android:id="@android:id/empty" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@id/header" 
      android:gravity="center" /> 
</RelativeLayout> 

RecipeListFragment.java

public class RecipeListFragment extends ListFragment { 
    private TextView header; 
    private TextView empty; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.common_list, container, false); 
     header = (TextView) v.findViewById(R.id.header); 
     empty = (TextView) v.findViewById(android.R.id.empty); 
     header.setText(R.string.header_recipe); 
     empty.setText(R.string.empty_recipes); 
     return v; 
    } 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     // List code here 
    } 
} 

此代码只有一个标题行,但您可以将该TextView更改为TableLayout,然后以相同的方式填充它,为您提供您的标题表并将其列表放在它下面,以便它不会滚动在它的顶部。

希望这会有所帮助!