如何使布局和视图的背景透明?

时间:2011-11-09 15:35:21

标签: android layout view transparency

我正在尝试使用背景制作布局,并在此布局的底部添加滚动视图。

这是我的代码:

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,我会在整个屏幕上看到背景

* EDIT2:

我编辑代码:删除不必要的布局,并设置背景可绘制,alpha设置为0,现在它可以工作。

好吧,如果有人向我解释为什么我需要这样做,我会很高兴的?

3 个答案:

答案 0 :(得分:2)

试试这个:

findViewById(R.id.v).setBackgroundColor(getResources().getColor(android.R.color.transparent));

答案 1 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
  </style>
</resources>

然后在清单

<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">
...
</activity>

答案 2 :(得分:0)

添加以下属性

android:background="@android:color/transparent"