android 动态设置actionbar背景色(代码修改)

一、将十六进制颜色值转为int类型color
  • int color = Color.parseColor("#ff00ff");
二、将int类型color值转为Drawable类型
  • ColorDrawable drawable = new ColorDrawable(color);
三、将drawable设置给actionbar
  • actionBar.setBackgroundDrawable(drawable);
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以通过以下步骤在Android中将ActionBar的标题居中: 1. 创建一个自定义布局文件,例如:custom_actionbar.xml,其中包含一个TextView元素,用于显示标题。 ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:id="@+id/action_bar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" android:gravity="center" android:textColor="@color/colorAccent" android:textSize="@dimen/text_size_large" android:textStyle="bold"/> </LinearLayout> ``` 2. 在Activity的onCreate方法中,使用setCustomView()方法将自定义布局设置ActionBar的视图。 ``` @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayShowCustomEnabled(true); actionBar.setCustomView(R.layout.custom_actionbar); TextView title = actionBar.getCustomView().findViewById(R.id.action_bar_title); title.setText("标题居中"); } ``` 在此示例中,我们使用了LinearLayout来垂直居中TextView元素,然后将TextView的重心设置为中心。您可以自定义布局来满足您的需求,并使用相应的布局属性来实现居中效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值