在android中,水平图像列表视图可以重叠
以下是我预期的布局: 如您所见,此滑块一次仅显示一个完整图像,其他图像重叠。 所有图像都是从代码中动态添加的。我尝试使用在android中,水平图像列表视图可以重叠,android,Android,以下是我预期的布局: 如您所见,此滑块一次仅显示一个完整图像,其他图像重叠。 所有图像都是从代码中动态添加的。我尝试使用LinearLayout作为所有图像的容器,但所有图像都溢出了屏幕 <LinearLayout android:id="@+id/llHorizontalImages" android:layout_width="match_parent" android:layout_height="wrap_content" android:ori
LinearLayout
作为所有图像的容器,但所有图像都溢出了屏幕
<LinearLayout
android:id="@+id/llHorizontalImages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
</LinearLayout>
我不知道如何使图像与其他图像重叠,如何保持图像不溢出父视图当列表中有许多图像时,它们应在更多照片视图中计为数字。有人有什么建议吗?看看。它允许您指定要显示子视图的确切位置。如果您指定这些距离足够近,则应该可以获得所需的效果。其余的(确保没有溢出,添加“更多照片”按钮)应该只是附加逻辑。这个剪贴网是实现您想要的东西的线索。你的问题让我很感兴趣,我试图得到同样的结果,但这是一种方法,并不完整 活动课 水平成像法 可牵引
注意:android:bottomRightRadius
和android:bottomLeftRadius
在API中有一个低于12的bug(SDK 3.1)。您应该在dimens.xml
内设置半径,并创建一个新文件夹values-v12
。请参阅此解决方案:
所有线程都在主线程
中,没有任何单独的线程
或异步任务
,您应该创建一些并行线程以避免性能不佳。我在NexusOne(SDK 2.2)和GalaxyNexus(3.2)上测试了这一点。这给了我:
我希望这会有用。注意。我认为,与其试图使图像相互重叠,不如将第一幅图像设置为其全宽/全高,然后创建一个扩展的ImageView类,帮助将其他图像显示为其半宽。我需要进一步了解您想要实现的目标。您需要水平列表视图(我指的是可滚动列表)吗?或者你不想,你只是想用一个按钮来显示重叠在单个列表中的图像,通知有更多的项目?@Fllo我是说第二个:图像可以在单个列表中重叠,但适合屏幕大小(宽度)。我不认为
RelativeLayout
是一个好主意,因为每个子视图都依赖于其他视图(通过id),如何在代码中动态生成?根据你的提示,FrameLayout
应该是一个不错的选择,但我不知道如何使它适合屏幕宽度。你说他们依靠别人的ID是什么意思?关于FrameLayout,我有什么建议吗?:P FrameLayout只用于显示一个项目,而不是多个ImageView,我认为动态生成它实际上要容易得多。您只需在图像中循环并将100dp(例如)添加到其左侧位置。这样,每个图像的右侧将多100 dp。
for (ImageInfo image : imagesList) {
ImageView imageView = new ImageView(context);
// load image from internet
ImageLoader.getInstance().displayImage(image.getImagePath(), imageView);
llHorizontalImages.addView(imageView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
int wScreen; // WIDTH 1 (Screen width)
RelativeLayout mLayout, mTextLayout; // CONTAINER (Parent's ImageViews)
ImageView imageView; // VAR IMAGEVIEW
private Integer[] imageArray = { // ARRAY IMAGES ITEMS
R.drawable.img1, R.drawable.img2, // (My test was with 18 items)
R.drawable.img3, R.drawable.img4,
R.drawable.img5, R.drawable.img6,
... };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Init container
mLayout = (RelativeLayout) findViewById(R.id.mContainer);
// Init subcontainer
mTextLayout = (RelativeLayout) findViewById(R.id.mTextCont);
// Get width screen (int)
DisplayMetrics display = this.getResources().getDisplayMetrics();
wScreen = display.widthPixels;
// Call the method
horizontalListImage();
}
private void horizontalListImage() {
int wImage, // WIDTH 2 (Images width)
mChild, // CHILD (Count Images)
n1, // LOOP 1 (Creation Images)
n2; // LOOP 2 (BringToFont function)
// Image width/height
wImage = wScreen / 5;
// Init images
for(n1 = 0; n1 < imageArray.length; n1++) {
// Create new images
imageView = new ImageView(this);
imageView.setImageResource(imageArray[n1]);
// Create a new params
RelativeLayout.LayoutParams paramsImage =
new RelativeLayout.LayoutParams(wImage,wImage);
// Start positioning Images from Left [1]
paramsImage.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
// Except first Image
if(n1 != 0) {
// Set margins left to Images [2]
paramsImage.setMargins((wImage/2)*n1,0,0,0);
}
// Add Images into Container
mLayout.addView(imageView,paramsImage);
/**
* [1] Images position from Left to Right: 0, 1, 2, 3, 4, 5..
* [2] Set margin left to positioning Image overlap each other
**/
// Screen have too much images
if( ((wImage/2)*(n1+4)) >= wScreen && n1+1 < imageArray.length ) {
break;
}
}
// Count children (Images) into container (RelativeLayout)
mChild = mLayout.getChildCount();
// Bring to front function [3]
for(n2 = mChild - 1; n2 >= 0; n2--) {
mLayout.getChildAt(n2).bringToFront();
mLayout.getChildAt(n2).invalidate();
/**
* [3] Bring to front method retrieve any Images in
* descendant order and replace them one by one to the front
* Images position from Left to Right: ..5, 6, 4, 3, 2..
*
* Result: The first Image (now end position) comes to front
* and End Image (now start position) comes to background
*
**/
}
// Number items total - number items displayed
int mNbInfo = imageArray.length - (mChild-1);
if(mNbInfo != 0) {
// Change width/height for text
RelativeLayout.LayoutParams paramsButton =
(RelativeLayout.LayoutParams) mTextLayout.getLayoutParams();
paramsButton.height = paramsButton.width = wImage;
mTextLayout.setLayoutParams(paramsButton);
// Display text
mTextLayout.setVisibility(View.VISIBLE);
// Update text
((TextView) findViewById(R.id.btInfoText))
.setText(mNbInfo+" More Photos");
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >
<RelativeLayout
android:id="@+id/mTextCont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/button_background"
android:visibility="gone" >
<TextView
android:id="@+id/btInfoText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:textColor="#ffc4c4c4"
android:textSize="12sp"
android:padding="5dip"
android:gravity="center" />
</RelativeLayout>
</RelativeLayout>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="5dip"
android:topLeftRadius="0dip"
android:bottomLeftRadius="@dimen/corner_bottom_left"
android:bottomRightRadius="@dimen/corner_bottom_right" />
<stroke
android:width="1dip"
android:color="#ffc4c4c4" />
<solid
android:color="#ffffffff" />
</shape>