【发布时间】:2011-11-09 15:35:21
【问题描述】:
我正在尝试制作一个带有背景的布局,并在此布局的底部添加一个滚动视图。
这是我的代码:
FrameLayout mainLayout = new FrameLayout(getApplicationContext());
mainLayout.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mainLayout.setBackgroundResource(R.drawable.background2);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, 200);
params.gravity = Gravity.BOTTOM|Gravity.CENTER;
final ScrollView scrollView = new ScrollView(getApplicationContext());
scrollView.setLayoutParams(params);
scrollView.setBackgroundColor(Color.TRANSPARENT);
StorageView storageView = new StorageView(getApplicationContext());
storageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Drawable drawable = getResources().getDrawable(R.drawable.background1);
drawable.setAlpha(0);
storageView.setBackgroundDrawable(drawable);
scrollView.addView(storageView);
mainLayout.addView(scrollView);
setContentView(mainLayout);
为什么我只看到背景图片?
*编辑:
如果我删除所有 setBackgroundColor 并将 setBackgroundDrawable 移动到 ScrollLayout 或 StorageView 我会在整个屏幕上看到背景
*编辑2:
我编辑了代码:我删除了不必要的布局,并设置了一个 alpha 设置为 0 的背景可绘制对象,现在它可以工作了。
好吧,如果有人向我解释为什么我需要这样做,我会很高兴?
【问题讨论】:
-
为什么不在 XML 编辑器中编写布局?首先让它看起来正确,然后根据需要动态创建它。
-
因为 StorageView 是一个自定义的 View,我在它的 onDraw 方法中绘制东西。所以我在 xml 上看不到它
-
但是StorageView上面的布局输出正确吗?我的意思是让它在你的 StorageView 之前工作。如果你已经有了,那么就没有必要了。 :)
-
正如我在问题中提到的,如果我将背景可绘制对象设置为 storageView 或 scrollLayout,我看到了
标签: android layout view transparency