我在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>
答案 0 :(得分:1)
尝试将重力放到像这样的ImageView,并更好地使用RelativeLayout而不是linearLayout:
android:layout_gravity="center_horizontal|center_vertical"
答案 1 :(得分:0)
您可以将标题图像转换为9-patch或XML drawable,以便它可以适应操作栏的大小,并将其用作操作栏 background。
请注意,使用此解决方案,如果您有大量可见的操作项,则可能会覆盖标题图像。
XML drawable的示例:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="@color/actionbar_color"/>
</shape>
</item>
<item>
<bitmap
android:src="@drawable/title_image"
android:gravity="center"/>
</item>
</layer-list>