[英]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.