滚动视图工作不正常

问题描述:

RelativeLayout layout; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    layout = new RelativeLayout(this); 
    setContentView(layout); 

    ScrollView sc = new ScrollView(this); 
    sc.setBackgroundColor(Color.YELLOW); 
    sc.setLayoutParams(layoutParams(300, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.ALIGN_PARENT_RIGHT)); 

    RelativeLayout rl = new RelativeLayout(this); 
    rl.setBackgroundColor(Color.CYAN); 
    rl.setLayoutParams(layoutParams(300, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.ALIGN_PARENT_RIGHT)); 


    int k=0; 
    for(int i=0;i<15;i++){ 
     ImageView iv1 = new ImageView(this); 
     iv1.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher)); 
     iv1.setLayoutParams(layoutParams(200, 200,0)); 
     iv1.setTranslationY(k); 
     k+=200; 
     rl.addView(iv1); 
    } 
    sc.addView(rl); 
    layout.addView(sc); 
} 

public RelativeLayout.LayoutParams layoutParams(int width,int height,int rule1){ 
    RelativeLayout.LayoutParams lpb = new RelativeLayout.LayoutParams(width,height); 
    if(rule1 != 0){ 
     lpb.addRule(rule1); 
    } 
    return lpb; 
} 

Output of code滚动视图工作不正常

当我使用的RelativeLayout而不滚动型则表示所有图像,但是当我使用滚动型则它没有显示所有图像。
请检查代码中的问题在哪里。

它应该在ScrollView内右对齐。 Desired output

所需输出的XML代码,我试图在java代码中进行转换。

<ScrollView 
    android:layout_width="300px" 
    android:layout_height="match_parent" 
    android:layout_alignParentRight="true" > 

    <RelativeLayout 
     android:layout_width="300px" 
     android:layout_height="fill_parent" 
     android:background="@android:color/holo_purple" > 

     <ImageView 
      android:id="@+id/iv1" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:src="@drawable/ic_action_home" /> 

     <ImageView 
      android:id="@+id/iv2" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:layout_below="@+id/iv1" 
      android:src="@drawable/ic_action_home" /> 

     <ImageView 
      android:id="@+id/iv3" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:layout_below="@+id/iv2" 
      android:src="@drawable/ic_action_home" /> 

     <ImageView 
      android:id="@+id/iv4" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:layout_below="@+id/iv3" 
      android:src="@drawable/ic_action_home" /> 
    </RelativeLayout> 
</ScrollView> 
+0

张贴它应该看起来的图像。 –

+0

添加了所需的输出截图。 –

+0

使用RecyclerView重复滚动内容。 –

一个滚动型的FrameLayout,这意味着你应该把一个孩子包含的全部内容,以滚动它;这个孩子本身可能是一个ayout经理复杂的对象层次结构

所以,当你使用相对布局它显示的图像,但是当你使用它亘古不变的包含下一个布局是亘古不变的工作,所有的图像滚动视图,所以尽量把所有图像下的LinearLayout(推荐)或其他布局。

+0

但是,relativeLayout具有所有图像,然后我将relativeLayout添加到滚动视图,因此理想情况下它应该显示带有滚动条的所有图像。因为相同的设置在Bottom_Aligned时正在工作。 –

+1

Bottom align不适用于ScrollView中的RelativeLayout。 ScrollView是无限的,它没有底部。 match_parent被wrap_content覆盖。 –

+0

from Bottom align,我的意思是完整的设置(在relativelayout中水平添加图片,并向scrollview添加relativelayout,然后将scrollview添加到屏幕底部)。 –