2016-04-27 70 views
0

我想将公司标志放在ActionBar的中心(绝对屏幕中心)。如何精确计算屏幕的中心点(按宽度)?在屏幕中心的ActionBar上放置图像

活动码:

ActionBar actionBar = getSupportActionBar(); 
     LayoutInflater inflator = (LayoutInflater) this 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = inflator.inflate(R.layout.action_bar_layout, null); 
     actionBar.setDisplayShowTitleEnabled(false); 
     actionBar.setDisplayShowCustomEnabled(true); 
     actionBar.setCustomView(v); 

action_bar_layout.xml:

<?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:gravity="center"> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/common_google_signin_btn_icon_dark_focused"/> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 

</LinearLayout> 
  • 我已经放在该图标例如。 enter image description here

有什么想法如何做到这一点?

回答

0

简单的答案是,您应该使用RelativeLayout而不是LinearLayout并在ImageView上设置android:layout_centerVertical="true"。跳过两个占位符视图。

我会建议你使用ToolBar,而不是(它需要你使用支持库)。将RelativeLayout放置在ToolBar中,宽度= height = match_parent。然后在您的活动中,执行以下操作:

mToolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(mToolbar); 
+0

我意识到现在可能您的意思是如何将图标置于后退按钮和最左侧动作图标的中间? – Joakim

+0

LinearLayout似乎也是如此。问题是将图像放置在屏幕的中心,而不是布局的中心。 – brut

+0

如果你想要的图像在屏幕的中心,它会工作,如果你尝试我建议的工具栏解决方案。 – Joakim