繁体   English   中英

将图像放在屏幕中央的ActionBar上

[英]Place image on the ActionBar on the center of the screen

我想将公司徽标放在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>
  • 例如,我已放置此图标。 在此处输入图片说明

有任何想法如何做到这一点?

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

我建议您改用ToolBar (这需要您使用支持库)。 将RelativeLayout放置在ToolBar中,宽度为width = height = match_parent。 然后在您的活动中,执行以下操作

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM