[英]Place image on the ActionBar on the center of the screen
I want to place company logo on the center of ActionBar (absolutely center of the screen). 我想将公司徽标放在ActionBar的中心(绝对在屏幕的中心)。 How can I calculate central point (by width) of the screen precisely?
如何精确计算屏幕的中心点(按宽度)?
Activity code: 活动代码:
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: 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>
Have any ideas how to do this? 有任何想法如何做到这一点?
The Simple Answer is that you should use a RelativeLayout
instead of LinearLayout
and set android:layout_centerVertical="true"
on the ImageView
. 简单的答案是,您应该使用
RelativeLayout
而不是LinearLayout
并在ImageView
上设置android:layout_centerVertical="true"
。 Skip the two placeholder views. 跳过两个占位符视图。
I would Advise you to use a ToolBar
instead though (Which requires you to use the support library). 我建议您改用
ToolBar
(这需要您使用支持库)。 Place the RelativeLayout inside the ToolBar with width=height=match_parent. 将RelativeLayout放置在ToolBar中,宽度为width = height = match_parent。 Then in your activity, do the following
然后在您的活动中,执行以下操作
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.