说明: 我想在工具栏上应用渐变。我已经做了。我在style.xml中的应用程序主题上应用了这个渐变。
这是我的style.xml
<resources>
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
</style>
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">@drawable/toolbar_gradient</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="MyMaterialTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="MyMaterialTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="MyTabLayoutTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse">
<item name="android:textSize">16sp</item>
<item name="android:textAllCaps">true</item>
</style>
</resources>
这是我的gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:type="linear"
android:startColor="@color/upper"
android:endColor="@color/bottom"
android:angle="270"/>
</shape>
这是我的color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="main_background">#FFFFFF</color>
<!--009788 26A69A-->
<color name="colorPrimary">#009788</color>
<color name="colorPrimaryDark">#E53935</color><!--00695C-->
<color name="dull_color">#26A69A</color>
<color name="colorAccent">#3F51B5</color>
<color name="textColorPrimary">#FFFFFF</color>`
<color name="txtColor">#000000</color>
<color name="windowBackground">#FFFFFF</color>
<color name="navigationBarColor">#000000</color>
<color name="counter_text_bg">#f32f32</color>
<color name="list_background">#FFFFFF</color>
<color name="sub_header">#CAF2E9</color>
<color name="subHeader_text_colors">#FFFFFF</color>
<color name="summary_sub_header">#EFF5F5</color>
<color name="hor_line">#B2DFDB</color>
<color name="spinner_bg_color">#B2DFDB</color>
<color name="summary">#616161</color>
<color name="score_header">#555555</color> <!--#919191-->
<color name="upper">#E15E5E</color>
<color name="bottom">#991D1D</color>
<color name="tab_background">#FFFFFF</color>
<color name="indicator_color">#009788</color> <!--#80CBC4-->
<color name="tab_text">#000000</color> <!--#FFFFFF-->
</resources>
这是我的splashScreen.java
public class SplashScreen extends AppCompatActivity {
public static String Response_code = "", main_public_access_token = "", device_id = "", key_access_token = "device_id", expires = "";
private static int statusCode;
long hoursdiff = 0, minutesdiff = 0, diff = 0, daydiff = 0;
String dateStart = "";
SharedPreferences pref2;
Date date1 = null;
Date date2 = null;
DateFormat dateFormatOld = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
device_id = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
dateFormatOld = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
if (!Utils.isNetworkConnected(getApplicationContext())) {
dialog_popup();
} else {
pref2 = this.getSharedPreferences("SaveTime", Context.MODE_PRIVATE);
if (pref2.getString("oldDate", null) != null) {
dateStart = pref2.getString("oldDate", null);
try {
date1 = dateFormatOld.parse(dateStart);
date2 = dateFormatOld.parse(dateFormatOld.format(new Date()));
diff = date2.getTime() - date1.getTime();
daydiff = diff / (24 * 60 * 60 * 1000);
hoursdiff = diff / (60 * 60 * 1000) % 24;
minutesdiff = diff / (60 * 1000) % 60;
if (daydiff == 0) {
Log.e("daydiff zero", "" + daydiff);
if (hoursdiff <= 22) {
Log.e("hours diff small 22", "" + hoursdiff);
Log.e("22 TOKEN", "" + pref2.getString("accessToken", null));
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(getBaseContext(),
MainActivity.class);
i.putExtra(key_access_token, pref2.getString("accessToken", null));
startActivity(i);
finish();
}
}, 2000);
} else if (hoursdiff == 23) {
Log.e("hour equal 22", "" + hoursdiff);
if (minutesdiff <= 55) {
Log.e("55 less", "" + pref2.getString("accessToken", null));
Log.e("minutes less 55", "" + minutesdiff);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(getBaseContext(),
MainActivity.class);
i.putExtra(key_access_token, pref2.getString("accessToken", null));
startActivity(i);
finish();
}
}, 2000);
} else {
Log.e("hour more 22", "" + minutesdiff);
new ResponseService().execute();
}
}
} else if (daydiff >= 1) {
Log.e("day more 1", "" + daydiff);
new ResponseService().execute();
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.e("pref zero", "DONE");
new ResponseService().execute();
}
}
}
}
当我安装我的应用程序时,此应用程序无法启动并出现错误。
FATAL EXCEPTION: main
Process: com.angelnx.cricvilla.cricvilla, PID: 21671
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.angelnx.cricvilla.cricvilla/com.angelnx.cricvilla.cricvilla.SplashScreen}: android.content.res.Resources$NotFoundException: File res/drawable/toolbar_gradient.xml from color state list resource ID #0x7f0200e7
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3190)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3300)
at android.app.ActivityThread.access$1000(ActivityThread.java:211)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6946)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
答案 0 :(得分:5)
colorPrimary
应该被赋予一种颜色(实际上只是一个整数)并且您使用的是ShapeDrawable
。
如果您想在工具栏背景中使用gradient.xml
,只需在工具栏背景上使用它即可。
将颜色更改为实际颜色
<item name="colorPrimary">@color/colorPrimary</item>
并在工具栏上将drawable作为背景:
<android.support.v7.widget.Toolbar
... parameters...
android:background="@drawable/gradient"
/>