如何创建随机的 Actionbar 和状态栏 Oncreate 模式
How to create random Actionbar and status bar Oncreate mode
我想在 onCreate
中随机创建 Actionbar
和 Status bar
,具有指定的颜色(橙色、绿色和粉色)。我在下面找到了一些有用的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar;
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#879f38")));
这对我有用,你也可以看到一些实验代码被注释掉了。我使用 AppCompatActivity。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
setupActionBar();
}
private void setToolbarColor() {
final int toolbarColor = getResources().getColor(R.id.color_orange);
// create our manager instance after the content view is set
final SystemBarTintManager tintManager = new SystemBarTintManager(this);
// enable status bar tint
tintManager.setStatusBarTintEnabled(true);
// enable navigation bar tint
tintManager.setNavigationBarTintEnabled(true);
// set a custom tint color for all system bars
tintManager.setTintColor(toolbarColor);
// // set a custom navigation bar resource
// tintManager.setNavigationBarTintResource(R.drawable.my_tint);
// // set a custom status bar drawable
// tintManager.setStatusBarTintDrawable(MyDrawable);
final ActionBar actionBar = getSupportActionBar();
if (null != actionBar) {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Window.setStatusBarColor();
// getWindow().setStatusBarColor(Color.BLUE);
// }
actionBar.setBackgroundDrawable(new ColorDrawable(toolbarColor));
}
}
您可以简单地在 color.xml 中创建颜色数组,然后从中随机选择一种颜色来设置操作栏颜色和状态栏颜色。
color.xml
<array name="actionbar_color">
<item>@color/bright_pink</item>
<item>@color/red</item>
<item>@color/orange</item>
<item>@color/yellow</item>
<item>@color/chartreuse</item>
<item>@color/green</item>
<item>@color/spring_green</item>
<item>@color/cyan</item>
<item>@color/azure</item>
<item>@color/blue</item>
<item>@color/violet</item>
<item>@color/magenta</item>
</array>
在你的activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// further code
int[] actionbarColor = context.getResources().getIntArray(R.array.actionbar_color);
actionBar.setBackgroundDrawable(new ColorDrawable(getRandom(actionbarColor)));
}
public int getRandom(int[] array) {
int rnd = new Random().nextInt(array.length);
return array[rnd];
}
随机操作栏颜色(根据当天)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void setColor_of_Day() throws ParseException {
String day=get_Day(get_Date());
int colorid;
switch (day){
case "Sunday":
colorid=getResources().getColor(R.color.sunday);
break;
case "Monday":
colorid=getResources().getColor(R.color.monday);
break;
case "Tuesday":
colorid=getResources().getColor(R.color.tuesday);
break;
case "Wednesday":
colorid=getResources().getColor(R.color.wednesday);
break;
case "Thursday":
colorid=getResources().getColor(R.color.thursday);
break;
case "Friday":
colorid=getResources().getColor(R.color.friday);
break;
case "Saturday":
colorid=getResources().getColor(R.color.saturday);
break;
default:
return;
}
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(colorid));
getWindow().setStatusBarColor(colorid);
}
public String get_Day(String date) throws ParseException {
SimpleDateFormat format1=new SimpleDateFormat("dd/MM/yyyy");
Date dt1=format1.parse(date);
DateFormat format2=new SimpleDateFormat("EEEE");
String day=format2.format(dt1);
return day;
}
public String get_Date(){
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date data= Calendar.getInstance().getTime();
String date=dateFormat.format(data);
String formatted_Date=FormatDate(date);
return formatted_Date;
}
我想在 onCreate
中随机创建 Actionbar
和 Status bar
,具有指定的颜色(橙色、绿色和粉色)。我在下面找到了一些有用的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar;
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#879f38")));
这对我有用,你也可以看到一些实验代码被注释掉了。我使用 AppCompatActivity。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
setupActionBar();
}
private void setToolbarColor() {
final int toolbarColor = getResources().getColor(R.id.color_orange);
// create our manager instance after the content view is set
final SystemBarTintManager tintManager = new SystemBarTintManager(this);
// enable status bar tint
tintManager.setStatusBarTintEnabled(true);
// enable navigation bar tint
tintManager.setNavigationBarTintEnabled(true);
// set a custom tint color for all system bars
tintManager.setTintColor(toolbarColor);
// // set a custom navigation bar resource
// tintManager.setNavigationBarTintResource(R.drawable.my_tint);
// // set a custom status bar drawable
// tintManager.setStatusBarTintDrawable(MyDrawable);
final ActionBar actionBar = getSupportActionBar();
if (null != actionBar) {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Window.setStatusBarColor();
// getWindow().setStatusBarColor(Color.BLUE);
// }
actionBar.setBackgroundDrawable(new ColorDrawable(toolbarColor));
}
}
您可以简单地在 color.xml 中创建颜色数组,然后从中随机选择一种颜色来设置操作栏颜色和状态栏颜色。
color.xml
<array name="actionbar_color">
<item>@color/bright_pink</item>
<item>@color/red</item>
<item>@color/orange</item>
<item>@color/yellow</item>
<item>@color/chartreuse</item>
<item>@color/green</item>
<item>@color/spring_green</item>
<item>@color/cyan</item>
<item>@color/azure</item>
<item>@color/blue</item>
<item>@color/violet</item>
<item>@color/magenta</item>
</array>
在你的activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// further code
int[] actionbarColor = context.getResources().getIntArray(R.array.actionbar_color);
actionBar.setBackgroundDrawable(new ColorDrawable(getRandom(actionbarColor)));
}
public int getRandom(int[] array) {
int rnd = new Random().nextInt(array.length);
return array[rnd];
}
随机操作栏颜色(根据当天)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void setColor_of_Day() throws ParseException {
String day=get_Day(get_Date());
int colorid;
switch (day){
case "Sunday":
colorid=getResources().getColor(R.color.sunday);
break;
case "Monday":
colorid=getResources().getColor(R.color.monday);
break;
case "Tuesday":
colorid=getResources().getColor(R.color.tuesday);
break;
case "Wednesday":
colorid=getResources().getColor(R.color.wednesday);
break;
case "Thursday":
colorid=getResources().getColor(R.color.thursday);
break;
case "Friday":
colorid=getResources().getColor(R.color.friday);
break;
case "Saturday":
colorid=getResources().getColor(R.color.saturday);
break;
default:
return;
}
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(colorid));
getWindow().setStatusBarColor(colorid);
}
public String get_Day(String date) throws ParseException {
SimpleDateFormat format1=new SimpleDateFormat("dd/MM/yyyy");
Date dt1=format1.parse(date);
DateFormat format2=new SimpleDateFormat("EEEE");
String day=format2.format(dt1);
return day;
}
public String get_Date(){
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date data= Calendar.getInstance().getTime();
String date=dateFormat.format(data);
String formatted_Date=FormatDate(date);
return formatted_Date;
}