【发布时间】:2015-09-10 22:25:23
【问题描述】:
我有一个自定义操作栏,其中包含我希望居中的两个项目(蓝色按钮)。目前,他们稍微偏左一点。我想确保按钮在所有屏幕尺寸上居中。对我来说,最好的方法是什么?
截图如下:
这是我的 XML project_action_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="@+id/list"
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:background="@drawable/left_rounded_rect_selected"
android:src="@drawable/list_white"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
/>
<ImageButton
android:id="@+id/map"
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:background="@drawable/right_rounded_rect_deselected"
android:src="@drawable/map_blue"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
/>
</LinearLayout>
</RelativeLayout>
在我的活动中:
private void showActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
View view = getLayoutInflater().inflate(R.layout.project_action_bar, null);
actionBar.setCustomView(view, lp);
View v = actionBar.getCustomView();
}
我还在这里添加了刷新和下拉菜单:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.add(Menu.NONE, R.id.refresh, Menu.NONE, "refresh")
.setIcon(R.drawable.ic_action_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
SubMenu optionsMenu = menu.addSubMenu("Options Item");
optionsMenu.add(Menu.NONE, R.id.delete_project, Menu.NONE, "Delete Project")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
optionsMenu.add(Menu.NONE, R.id.logout, Menu.NONE, "Log Out")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
MenuItem optionsMenuItem = optionsMenu.getItem();
optionsMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
optionsMenuItem.setIcon(R.drawable.ic_action_overflow);
return super.onCreateOptionsMenu(menu);
}
【问题讨论】:
-
试一试 -- android:layout_gravity="center|center_vertical" 或者实际上可能不起作用 --try - android:layout_gravity="center|center_horizontal" 并尝试 -- android:layout_weight ="1" 按钮
标签: android xml layout android-actionbar