我的渐变没有出现在我的 Android 项目中

发布于 2024-09-30 13:33:01 字数 921 浏览 0 评论 0原文

我是 Android 平台的新手,我正在开发一个应用程序,其中主要活动 正在使用对话框主题。

我的用户界面设置几乎按照我想要的方式设置,但渐变似乎不适合我。 我尝试过渐变标题栏、视图、布局、textView 等,但在每种情况下渐变都不会显示。 (到目前为止我只在模拟器上测试过)。

我的可绘制 xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#00000000"
      android:centerColor="#FFFFFFFF"
      android:endColor="#00000000"
      android:angle="90" />
</shape>

我使用以下内容将背景设置为多个元素,但它从未显示:

android:background="@drawable/gradient_bg"

我尝试了不同的颜色和角度等,但无济于事。

如果我执行以下操作,它会起作用,但我更喜欢在 xml 中执行此操作。

GradientDrawable grad = new GradientDrawable(
      Orientation.TOP_BOTTOM,
      new int[] {Color.RED, Color.YELLOW}
    );
    this.getWindow().setBackgroundDrawable(grad);

有人可以帮忙吗? 谢谢。

I'm new to the Android platform and I'm developing an app that where the main activity
is using the dialog theme.

I have the ui setup pretty much the way I want but gradients don't seem to be working for me.
I've tried a gradient title bar, view, layout, textView etc and in every case the gradient does not display. (I've only tested it on the simulator so far).

My drawable xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#00000000"
      android:centerColor="#FFFFFFFF"
      android:endColor="#00000000"
      android:angle="90" />
</shape>

and I use the following to set the background to several elements but it never shows up:

android:background="@drawable/gradient_bg"

I've tried different colors and angles etc etc but to no avail.

If I do the following it works however I prefer to do this in xml.

GradientDrawable grad = new GradientDrawable(
      Orientation.TOP_BOTTOM,
      new int[] {Color.RED, Color.YELLOW}
    );
    this.getWindow().setBackgroundDrawable(grad);

Can anyone help?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溇涏 2024-10-07 13:33:01

尝试在整个渐变上设置不透明颜色。

例如,使用 #FF000000 表示黑色。前两个十六进制数字是 alpha 值。

Try setting opaque colors on the whole gradient.

For example, use #FF000000 for black. The two first hex numbers are the alpha value.

回心转意 2024-10-07 13:33:01

您的代码是:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#00000000"
      android:centerColor="#FFFFFFFF"
      android:endColor="#00000000"
      android:angle="90" />
</shape>

在 Android HTC Hero 手机上工作正常并正确显示成分!模拟器上可能无法显示渐变效果。

your code is:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#00000000"
      android:centerColor="#FFFFFFFF"
      android:endColor="#00000000"
      android:angle="90" />
</shape>

that is working fine and dispaying gredient correctly on Android HTC Hero phone !! It may be a case that gredient effect may not be showing up on Emulator.

素罗衫 2024-10-07 13:33:01

谢谢。我已经确定它可以在实际设备上运行,因此它一定是模拟器问题。

Thanks. I've determined that it works on an actual device so it must be an emulator issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文