我试图在水平滚动中显示两个视图,如下图所示。我想知道实现此目标的可能方法,因为我所实现的输出远远超出了要求,正如我们在下面的屏幕抓图中所看到的那样:-
这是我的布局的一部分,描述了我已经实现的视图:-
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/imageGallery"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"/>
</HorizontalScrollView>
然后,在上面的视图中,我创建了一个垂直方向的线性布局,并在其中添加了imageview和3个文本视图,如下面的代码片段所示:-
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private View getVerticalLinearLayout(Integer image){
LinearLayout verticalLayout = new LinearLayout(getContext());
verticalLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,2.0f));
verticalLayout.setOrientation(LinearLayout.VERTICAL);
verticalLayout.setBackground(getResources().getDrawable(R.drawable.layout_border));
verticalLayout.addView(scaleImage((ImageView) getImageView(image)));
verticalLayout.addView(getTextView("Book Name",14,"#444444"));
verticalLayout.addView(getTextView("Author Name",12,"#444444"));
verticalLayout.addView(getTextView("Lender Name",12,"444444"));
return verticalLayout;
}
您可以将书本视图设计为单独的布局文件。然后,您可以使用LayoutInflater实例化它,并用您的数据填充它,最后将它们分别添加到imageGallery
布局中。
要使两个视图一次恰好出现在屏幕上,您必须知道屏幕的宽度。这可以通过以下方法实现,DisplayMetrics
也可以调用getWidth()
完全水平覆盖屏幕的视图。一旦获得该宽度bookView
,将要添加到水平滚动视图的布局就需要将其宽度设置为screenWidth/2
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句