Android ActionBar,样式修改,常见问题



1:overflow 不显示

           

  一般的来说在Actionbar中在条目过多时会显示三个竖着的小点的菜单,但在实机测试的时候发现并不显示,上网查找了之后发现问题所在:如果该机器拥有实体的menu键则不在右侧显示溢出菜单,而改为按menu来生成。这样就不利于统一的界面风格。

我们可以改变系统探测实体menu键的存在与否来改变这个的显示。

菜单显示是根据public boolean hasPermanentMenuKey ()这个方法来判断的。这个方法是获取sHasPermanentMenuKey的boolean值。

解决办法:通过在onCreate()中

try {
ViewConfiguration mconfig = ViewConfiguration.get(this);
       Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
       if(menuKeyField != null) {
           menuKeyField.setAccessible(true);
           menuKeyField.setBoolean(mconfig, false);
       }
   } catch (Exception ex) {
   }

这样就大功告成了


2:ActionBar向左Id获取不到

   id是android.R.id.home而不是我们项目中的R.id.home


   显示向返回按钮

@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); 
		
		super.onActivityCreated(savedInstanceState);
	}
   监听事件

   

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {  
		if(item.getItemId() == android.R.id.home)
		{ 
			
		}
	    return true;
	} 

   


3:搜索框searchView图片显示过大

   是资源文件放置的问题,放到drawable-xxhdpi下边就可以了


4:searchView事件

	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		
	    MenuInflater inflater = getMenuInflater();  
	    inflater.inflate(R.menu.main, menu);  
	    MenuItem searchItem = menu.findItem(R.id.action_search);  
	    SearchView searchView = (SearchView) searchItem.getActionView();  	
	    
	    searchView.setOnQueryTextListener(new OnQueryTextListener () //搜索时会触发这个事件搜索即可
	    {

			public boolean onQueryTextSubmit(String query) {
				// TODO Auto-generated method stub
				return false;
			}

			public boolean onQueryTextChange(String newText) {
				 Toast.makeText(MainActivity.this,newText, Toast.LENGTH_SHORT).show();  
				return false;
			}  	
	    });
	    
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

5:actionbar的样式修改

     修改使用的style就行了,引用

      android:theme="@style/AppTheme" 

    

<style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

<style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
         <item name="android:textColor" >#ffffff</item>
    </style>
   但是由于父控件是android:Theme.Light,这样会会把所有的字体颜色都变成白色了

   

   其实只需要引用默认的actionbar样式就可以了,白底黑字

<style name="AppTheme" parent="android:style/Theme.Holo">
    </style>

    需要改变:

 <style name="AppTheme" parent="android:style/Theme.Holo">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <!--  <item name="android:textColor" >#ffffff</item> -->
        <!-- <item name="android:actionDropDownStyle">@style/MyDropDownNav</item>  -->
        <!-- <item name="android:textColor" >#ffffff</item>  -->  
                 <item name="android:actionDropDownStyle">@style/MyDropDownNav</item>         
    </style>
    
    

   <style name="MyDropDownNav" parent="android:style/Widget.Holo.Light.Spinner">
       <!--  <item name="android:background">#ff0000</item> -->
        <item name="android:colorForeground">#ffffff</item>
        <item name="android:textColor" >#ffffff</item> 
    </style>
http://www.apkbus.com/android-125933-1-1.html

http://blog.csdn.net/liu149339750/article/details/8282471



6:onOptionsItemSelected事件不回调

  必须要在onCreate设置setHasOptionsMenu(true);

   

public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);//添加菜单不调用该方法是没有用的
	}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值