【发布时间】:2015-06-03 11:50:09
【问题描述】:
我在 android 中有一个自定义操作栏,其图像必须在操作栏中居中。 “Brancom”图像必须在中间,但我无法让它工作,因为它旁边有一个汉堡包图标。我该如何解决?这是它的样子:http://gyazo.com/30eac941ea964c143012d470a2d76d3b
如您所见,它不在中心。这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/actionBarLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:clickable="false"
android:focusable="false"
android:longClickable="false"
android:src="@drawable/ic_brancom_actionbar" />
</LinearLayout>
这是我的java代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_custom_view_home);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
and then the rest of the code...
编辑
这就是正在发生的事情:http://gyazo.com/c014f55d10aa0932a9ccbaea97bae674
编辑#2
我通过添加这个来解决这个问题,显然图标是 60dp,所以我在将其设为相对布局后从中心减去它:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/actionBarLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:longClickable="false"
android:src="@drawable/ic_brancom_actionbar"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="60dp"
android:layout_marginEnd="60dp" />
</RelativeLayout>
【问题讨论】: