2013-08-17 91 views
10

我遇到了一个问题,当我尝试给一个LinearLayout的负左边距。
负边距不会出现。
这里是我的代码Android负边距不起作用

HorizontalScrollView hview = new HorizontalScrollView(context); // HorizontalScrollView is the outer view 
RelativeLayout.LayoutParams hs_lot_params = new RelativeLayout.LayoutParams(164, 164); 
hs_lot_params.setMargins(100, 100, 0, 0); // set the positions 

ImageView image = new ImageView(context); 
image.setBackgroundResource(R.drawable.leder); 
LinearLayout.LayoutParams img_lot_params = new LinearLayout.LayoutParams(164, 164); 
img_lot_params.setMargins(0, 0, 0, 0); 

LinearLayout ll = new LinearLayout(this); 
ll.setOrientation(LinearLayout.VERTICAL); 
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(164, 164); 
layoutParams.setMargins(-132, 0, 0, 0); 
ll.addView(image, img_lot_params); 
hview.addView(ll, layoutParams); 

注:我的计划是从左至右滚动图像。
首先,图像的左边部分是隐藏的,可以滚动到右侧看到完整的图像

+2

原因是因为在之前的Android SDK版本中,允许使用负边界(最高2.2?)。 –

+1

那我们如何替换AbsoluteLayout而不使用负边距? –

+3

使用'translateX'。 – nhaarman

回答

0

负利润率应该在LinearLayoutRelativeLayout工作。您可能需要的是滚动HorizontalScrollViewscrollBy(int x, int y)scrollTo(int x, int y)以实现您所描述的“窥视和滚动”效果。

另外请记住,使用原始像素单位通常是一个坏主意,因为实际大小将取决于屏幕的像素密度。优先选择dp测量。

0
ViewGroup.MarginLayoutParams params = 
(ViewGroup.MarginLayoutParams)view.getLayoutParams(); params.topMargin = -100; 
+0

您是否可以编辑您的答案解释如何解决这个问题.. –