【问题标题】:Centering Custom Action Bar Items居中自定义操作栏项目
【发布时间】: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_horizo​​ntal" 并尝试 -- android:layout_weight ="1" 按钮

标签: android xml layout android-actionbar


【解决方案1】:

按钮在自定义视图中居中,但自定义视图不在操作栏中居中。

自定义视图填充主页图标和菜单操作之间的空间。如果菜单操作比主页图标占用更多空间(就像您的情况一样),自定义视图将不会在操作栏中居中。

如果你真的想实现这一点,我的建议是获取屏幕的宽度和主页图标的宽度,然后做一些数学计算来确定左边距应该是多少才能使按钮出现居中。

您也许可以在某处的样式属性中找到主页图标的宽度(因为不同屏幕尺寸的宽度不同)。

我不想弄清楚。我只需更改按钮颜色以匹配菜单操作图标并将它们右对齐。或者也许做一个自定义菜单操作视图/操作提供程序而不是自定义标题视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    相关资源
    最近更新 更多