相关疑难解决方法(0)

如何在自定义标题栏上以编程方式设置背景颜色渐变?

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

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

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.foo_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar); 
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

如您所见,线性布局的背景由此人定义:

<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>
Run Code Online (Sandbox Code Playgroud)

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

如果您有更好的方法来设置背景渐变,我会对所有想法持开放态度.

先感谢您!!

android gradient background android-layout

64
推荐指数
1
解决办法
7万
查看次数

Android创建类似iphone的Gradient

我需要在我的Android应用程序中创建像渐变这样的黑色iphone.请在下图中查看顶部的黑色渐变.怎么做?谢谢

替代文字

android gradient

5
推荐指数
2
解决办法
9884
查看次数

标签 统计

android ×2

gradient ×2

android-layout ×1

background ×1