【问题标题】:How do I programmatically set the background color gradient on a Custom Title Bar?如何以编程方式在自定义标题栏上设置背景颜色渐变?
【发布时间】:2011-09-01 05:22:34
【问题描述】:

那里有很多关于实现自定义标题栏的教程和问题。但是,在我的自定义标题栏中,我有一个自定义背景渐变,我想知道如何在我的代码中动态设置它。

这里是我的自定义标题栏被调用的地方:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.foo_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar); 

这是我的custom_title_bar

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@layout/custom_title_bar_background_colors">
<ImageView   
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:src="@drawable/title_bar_logo"
              android:gravity="center_horizontal"
              android:paddingTop="0dip"/>

</LinearLayout>

如你所见,线性布局上的背景是由这个人定义的:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient 
    android:startColor="#616261" 
    android:endColor="#131313"
    android:angle="270"
 />
<corners android:radius="0dp" />
</shape>

我想做的是在我的代码中动态设置这些渐变颜色。我不想像现在这样在我的 XML 文件中硬编码它们。

如果您有更好的方法来设置背景渐变,我愿意接受所有想法。

提前谢谢你!!

【问题讨论】:

    标签: android android-layout background gradient


    【解决方案1】:

    要在代码中执行此操作,您需要创建一个 GradientDrawable。
    设置角度和颜色的唯一机会是在构造函数中。 如果你想改变颜色或角度,只需创建一个新的 GradientDrawable 并将其设置为背景

        View layout = findViewById(R.id.mainlayout);
    
        GradientDrawable gd = new GradientDrawable(
                GradientDrawable.Orientation.TOP_BOTTOM,
                new int[] {0xFF616261,0xFF131313});
        gd.setCornerRadius(0f);
    
        layout.setBackgroundDrawable(gd);
    

    为此,我在您的主 LinearLayout 中添加了一个 ID,如下所示

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mainlayout"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <ImageView   
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:src="@drawable/title_bar_logo"
                  android:gravity="center_horizontal"
                  android:paddingTop="0dip"/>
    
    </LinearLayout>
    

    并将其用作自定义标题栏

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title_bar);
        View title = getWindow().findViewById(R.id.mainlayout);
        title.setBackgroundDrawable(gd);
    

    【讨论】:

    • 感谢您的提示。这可以为小部件完成吗?我有一个具有渐变形状的小部件,我想更改此渐变的颜色。在小部件的情况下这可能吗?
    • @slund 有没有办法从中心产生渐变?而不是使用 'GradientDrawable.Orientation.TOP_BOTTOM?'
    • 没关系,但是如果用户的API
    • @mark 这是您问题的答案stackoverflow.com/a/11947755/3080016
    • setBackgroundDrawable() 已被弃用,因为我认为 API 16。您也应该使用setBackground()。见stackoverflow.com/questions/27141279/…
    【解决方案2】:

    如果接受的答案不起作用,您可以尝试设置foreground

    color.setForeground(gradient);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-28
      • 2014-10-25
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多