ActionBar-带有居中ImageView的自定义视图,侧面有操作项

ActionBar-带有居中ImageView的自定义视图,侧面有操作项

我需要将“首页”活动的自定义徽标(使用ImageView)居中在操作栏中。我正在为此项目使用ABS。这与SO上发布的另一个问题(ActionBar徽标居中,Action项位于侧面)非常相似,但是我不确定ImageView或搜索菜单是否有所作为,因为我没有得到想要的结果(以居中的图片为准),或者如果我刚弄错的话。基本上,我在左侧设置一个图标,在中心插入自定义视图,在右侧添加一个搜索图标(OptionsMenu)。该图像的确确实显示在图标的右侧,但是仍然居中。任何有关如何在操作栏中居中放置ImageView的指针将不胜感激。


Home.java:


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_home);


    LayoutInflater inflater = (LayoutInflater) getSupportActionBar().getThemedContext()

            .getSystemService(LAYOUT_INFLATER_SERVICE);


    final View customActionBarView = inflater.inflate(

            R.layout.actionbar_custom_view_home, null);


    /* Show the custom action bar view and hide the normal Home icon and title */

    final ActionBar actionBar = getSupportActionBar();

    actionBar.setHomeButtonEnabled(true);

    actionBar.setDisplayHomeAsUpEnabled(false);

    actionBar.setDisplayShowTitleEnabled(false);

    actionBar.setIcon(R.drawable.ic_ab_som);

    actionBar.setCustomView(customActionBarView);

    actionBar.setDisplayShowCustomEnabled(true);


}


@Override

public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = new MenuInflater(this);

    inflater.inflate(R.menu.search, menu);

    return true;

}

res / layout / actionbar_custom_view_home.xml:


<?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:orientation="horizontal"

android:layout_gravity="center">


<ImageView

    android:id="@+id/actionBarLogo"

    android:contentDescription="@string/application_name"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:clickable="false"

    android:duplicateParentState="false"

    android:focusable="false"

    android:longClickable="false"

    android:padding="@dimen/padding_small"

    android:src="@drawable/logo_horizontal" />


</LinearLayout>


繁花不似锦
浏览 441回答 4
4回答

吃鸡游戏

代码中的ImageView相对于LinearLayout居中,而不是相对于操作栏居中。您可以将左边缘(android:layout_marginLeft)添加到布局中以调整图像位置。这样做的另一种方法不是在操作栏中添加图标和操作项,而是使用带有图标和按钮的自定义布局。但是在这种情况下,您将需要自己处理操作项。
打开App,查看更多内容
随时随地看视频慕课网APP