Android如何使用自定义按钮和自定义背景图像创建自定义ActionBar

时间:2016-07-21 03:11:33

标签: android android-actionbar

我是Android的初学者。我想为我的应用程序创建一个自定义操作栏,带有自定义背景图像和自定义按钮,我已经尝试但结果并不完全像我想要的。 My custom action bar我想让我的自定义背景适合屏幕。我能怎么做? 这是我的custombar_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:background="@drawable/bar">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/back"
        android:scaleType="fitStart"/>

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_marginLeft="?attr/actionBarSize"
        android:layout_marginRight="?attr/actionBarSize"
        android:text="SFlashCard"/>

</LinearLayout>

在主要活动中:

ActionBar actBar = getSupportActionBar();

actBar.setDisplayShowHomeEnabled(false);
actBar.setDisplayShowTitleEnabled(false);
LayoutInflater inflater = LayoutInflater.from(this);
View customView = inflater.inflate(R.layout.custom_actionbar, null);
actBar.setCustomView(customView);


actBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actBar.setDisplayShowCustomEnabled(true);

非常感谢所有人的支持!

0 个答案:

没有答案
相关问题