【问题标题】:How to make layout's and view's background transparent?如何使布局和视图的背景透明?
【发布时间】: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


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:
    <?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>
    

    【讨论】:

    • @Lukap 先生,如何制作片段??
    【解决方案3】:

    添加以下属性

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-02
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      • 2020-03-18
      • 2011-06-25
      相关资源
      最近更新 更多