2013-03-11 112 views
5

当我尝试将我的自定义视图添加到水平滚动视图时,它不显示。 但相同的自定义视图显示在其他布局,如相对和线性布局。自定义视图不显示在水平滚动视图不工作

layout=new LinearLayout(this); 
     layout.setBackgroundColor(Color.YELLOW); 
     layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT)); 
     layout.setOrientation(LinearLayout.HORIZONTAL); 

     l = new PredicateLayout(this); 
//  l.sendtext(info); 
     for(int j=0;j<info.length;j++) 
     { 

      LinearLayout aa=new LinearLayout(this); 
      aa.setOrientation(LinearLayout.VERTICAL); 
      t = new TextView(this); 
       t.setText(info[j]); 
       t.setBackgroundColor(Color.GREEN); 
       t.setTextSize(i); 
       t.setWidth(screenwidth/2); 
       aa.addView(t); 
       l.addView(aa, new PredicateLayout.LayoutParams(0, 0)); 

     } 

     layout.addView(l); 
     scroll.addView(layout); 

我的XML看起来像这样

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scroll" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    android:orientation="horizontal" 
    > 

</HorizontalScrollView> 

也试过这样

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scroll" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    android:orientation="horizontal" 
    > 

    <com.test.app.PredicateLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:id="@+id/predicate_layout" 
     > 

    </com.omnion.bible.PredicateLayout> 
</HorizontalScrollView> 

代码看起来象

test=(PredicateLayout)findViewById(R.id.predicate_layout); 

for(int j=0;j<info.length;j++) 
     { 

      LinearLayout aa=new LinearLayout(this); 
      aa.setOrientation(LinearLayout.VERTICAL); 
      t = new TextView(this); 
       t.setText(info[j]); 
       t.setBackgroundColor(Color.GREEN); 
       t.setTextSize(i); 
       t.setWidth(screenwidth/2); 
       aa.addView(t); 
       test.addView(aa, new PredicateLayout.LayoutParams(0, 0)); 

     } 

任何一个可以帮助我。 提前致谢。

+0

,如果你想创建自定义查看您需要添加视图布局.. – Janmejoy 2013-03-11 12:38:48

回答

1

把你的自定义布局这样

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scroll" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    android:orientation="horizontal" 
    > 
<LinearLayout 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent"> 
    <com.test.app.PredicateLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:id="@+id/predicate_layout" 
     > 

    </com.omnion.bible.PredicateLayout> 
</LinearLayout> 
</HorizontalScrollView>