【发布时间】:2017-04-22 10:31:52
【问题描述】:
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;
}
当我使用没有 ScrollView 的 RelativeLayout 时,它会显示所有图像,但是当我使用 ScrollView 时,它不会显示所有图像。
请检查代码的问题在哪里。
它应该在 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>
【问题讨论】:
-
张贴一张应该是什么样子的图片。
-
添加了所需的输出屏幕截图。
-
使用 RecyclerView 重复滚动内容。
-
我想使用相对布局,因为它允许我使用重叠的图像。
标签: java android scrollview android-relativelayout