关于自定义ActionBar的问题,求大神解答

Sanwe 2014-07-25 02:22:03

请问怎样把ActionBar做成这种效果,用一个图标替换掉左侧的logo和向左导航的小箭头,在ActionBar居中添加文字
...全文
2073 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
JD_小贤 2016-04-07
  • 打赏
  • 举报
回复
android.support.v7.app.ActionBar.LayoutParams layout = new android.support.v7.app.ActionBar.LayoutParams(android.support.v7.app.ActionBar.LayoutParams.FILL_PARENT, android.support.v7.app.ActionBar.LayoutParams.FILL_PARENT); getSupportActionBar().setCustomView(view, layout); getSupportActionBar().setDisplayShowCustomEnabled(true); 试试这样,我测试过,没问题
mml_慢慢来 2016-03-24
  • 打赏
  • 举报
回复
我自定义的也不能居中
baidu_30778665 2015-11-05
  • 打赏
  • 举报
回复
您好:不能居中的问题你们解决没?我今天想用这个方法,发现一直不能居中,求大神指点。。。
FJekin 2015-11-05
  • 打赏
  • 举报
回复
楼主看下这个是不是你想要的。把属性值改一下 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.androidtest.MainActivity" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageButton android:id="@+id/imageButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/btn_star" /> <TextView android:id="@+id/arTitle" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="Title" android:gravity="center" android:textSize="16sp"/> </LinearLayout> </LinearLayout>
jklwan 2015-11-05
  • 打赏
  • 举报
回复
使用没有actionbar的样式,然后自己写头部样式,可以复用
sinat_17841263 2014-07-30
  • 打赏
  • 举报
回复
<?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="match_parent" android:orientation="horizontal" android:gravity="center_vertical" android:background="@color/maincolor"> <ImageButton android:id="@+id/arClose" android:layout_width="50dp" android:layout_height="44dp" android:src="@drawable/icon_back" android:onClick="onClick" android:background="@null" /> <TextView android:id="@+id/arTitle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="@color/white" android:text="Title" android:gravity="center" android:textSize="16sp" /> </LinearLayout>
lionfresh 2014-07-25
  • 打赏
  • 举报
回复
宽度和高度match_parent
Sanwe 2014-07-25
  • 打赏
  • 举报
回复
引用 8 楼 lionfresh 的回复:
[quote=引用 5 楼 bblaopoJun 的回复:] [quote=引用 4 楼 lionfresh 的回复:] [quote=引用 3 楼 bblaopoJun 的回复:] [quote=引用 1 楼 lionfresh 的回复:] 自定义ActionBar视图, getSupportActionBar().setCustomView(LayoutInflater.from(this).inflate(R.layout.title, null)); 其中title为自定义的布局文件。
请问这个布局文件是在drawable文件夹下定义吗 还有我的只能ActionBar actionBar = this.getActionBar(); 不能getSupportActionBar() 对功能有没有什么影响[/quote] 第一个问题,我写的是R.layout.title,当然是定义在layout下; 第二个问题,getSupportActionBar()是因为我使用一个开源库,用来兼容低版本的手机,你使用getActionBar()是没有问题的,就是3.0以下版本是不兼容的。[/quote] 谢谢 我的布局文件这样写的,但不知道为什么还是不能居中显示 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/about_actionbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="关于" android:textSize="18sp" /> </RelativeLayout>[/quote] android:gravity="center"加上这句[/quote] 在RelativeLayout里加了android:layout_gravity="center" 在TextView里加了android:gravity="center"都没用
lionfresh 2014-07-25
  • 打赏
  • 举报
回复
引用 5 楼 bblaopoJun 的回复:
[quote=引用 4 楼 lionfresh 的回复:] [quote=引用 3 楼 bblaopoJun 的回复:] [quote=引用 1 楼 lionfresh 的回复:] 自定义ActionBar视图, getSupportActionBar().setCustomView(LayoutInflater.from(this).inflate(R.layout.title, null)); 其中title为自定义的布局文件。
请问这个布局文件是在drawable文件夹下定义吗 还有我的只能ActionBar actionBar = this.getActionBar(); 不能getSupportActionBar() 对功能有没有什么影响[/quote] 第一个问题,我写的是R.layout.title,当然是定义在layout下; 第二个问题,getSupportActionBar()是因为我使用一个开源库,用来兼容低版本的手机,你使用getActionBar()是没有问题的,就是3.0以下版本是不兼容的。[/quote] 谢谢 我的布局文件这样写的,但不知道为什么还是不能居中显示 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/about_actionbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="关于" android:textSize="18sp" /> </RelativeLayout>[/quote] android:gravity="center"加上这句
Sanwe 2014-07-25
  • 打赏
  • 举报
回复
Sanwe 2014-07-25
  • 打赏
  • 举报
回复
引用 4 楼 lionfresh 的回复:
[quote=引用 3 楼 bblaopoJun 的回复:] [quote=引用 1 楼 lionfresh 的回复:] 自定义ActionBar视图, getSupportActionBar().setCustomView(LayoutInflater.from(this).inflate(R.layout.title, null)); 其中title为自定义的布局文件。
请问这个布局文件是在drawable文件夹下定义吗 还有我的只能ActionBar actionBar = this.getActionBar(); 不能getSupportActionBar() 对功能有没有什么影响[/quote] 第一个问题,我写的是R.layout.title,当然是定义在layout下; 第二个问题,getSupportActionBar()是因为我使用一个开源库,用来兼容低版本的手机,你使用getActionBar()是没有问题的,就是3.0以下版本是不兼容的。[/quote] Activity中的代码 super.onCreate(savedInstanceState); ActionBar actionBar = this.getActionBar(); actionBar.setHomeButtonEnabled(true); actionBar.setDisplayShowTitleEnabled(false); actionBar.setDisplayUseLogoEnabled(false); actionBar.setDisplayHomeAsUpEnabled(false); actionBar.setIcon(R.drawable.icon_back_btn_normal); setContentView(R.layout.about); /** * actionBar添加自定义view */ View actionbarLayout = LayoutInflater.from(this).inflate(R.layout.about_actionbar, null); actionBar.setDisplayShowCustomEnabled(true); actionBar.setCustomView(actionbarLayout);
Sanwe 2014-07-25
  • 打赏
  • 举报
回复
引用 4 楼 lionfresh 的回复:
[quote=引用 3 楼 bblaopoJun 的回复:] [quote=引用 1 楼 lionfresh 的回复:] 自定义ActionBar视图, getSupportActionBar().setCustomView(LayoutInflater.from(this).inflate(R.layout.title, null)); 其中title为自定义的布局文件。
请问这个布局文件是在drawable文件夹下定义吗 还有我的只能ActionBar actionBar = this.getActionBar(); 不能getSupportActionBar() 对功能有没有什么影响[/quote] 第一个问题,我写的是R.layout.title,当然是定义在layout下; 第二个问题,getSupportActionBar()是因为我使用一个开源库,用来兼容低版本的手机,你使用getActionBar()是没有问题的,就是3.0以下版本是不兼容的。[/quote] 谢谢 我的布局文件这样写的,但不知道为什么还是不能居中显示 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/about_actionbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="关于" android:textSize="18sp" /> </RelativeLayout>
lionfresh 2014-07-25
  • 打赏
  • 举报
回复
引用 3 楼 bblaopoJun 的回复:
[quote=引用 1 楼 lionfresh 的回复:] 自定义ActionBar视图, getSupportActionBar().setCustomView(LayoutInflater.from(this).inflate(R.layout.title, null)); 其中title为自定义的布局文件。
请问这个布局文件是在drawable文件夹下定义吗 还有我的只能ActionBar actionBar = this.getActionBar(); 不能getSupportActionBar() 对功能有没有什么影响[/quote] 第一个问题,我写的是R.layout.title,当然是定义在layout下; 第二个问题,getSupportActionBar()是因为我使用一个开源库,用来兼容低版本的手机,你使用getActionBar()是没有问题的,就是3.0以下版本是不兼容的。
Sanwe 2014-07-25
  • 打赏
  • 举报
回复
引用 1 楼 lionfresh 的回复:
自定义ActionBar视图, getSupportActionBar().setCustomView(LayoutInflater.from(this).inflate(R.layout.title, null)); 其中title为自定义的布局文件。
请问这个布局文件是在drawable文件夹下定义吗 还有我的只能ActionBar actionBar = this.getActionBar(); 不能getSupportActionBar() 对功能有没有什么影响
lionfresh 2014-07-25
  • 打赏
  • 举报
回复
然后再设置 getSupportActionBar().setDisplayShowCustomEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(false); getSupportActionBar().setDisplayShowTitleEnabled(false); 这几个属性
lionfresh 2014-07-25
  • 打赏
  • 举报
回复
自定义ActionBar视图, getSupportActionBar().setCustomView(LayoutInflater.from(this).inflate(R.layout.title, null)); 其中title为自定义的布局文件。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧