如何在Android

时间:2015-07-29 08:29:19

标签: android android-actionbar

我在Android应用中创建了一个自定义操作栏。不幸的是,我的自定义布局中没有正确设置高度和宽度。运行应用程序后,在自定义操作栏上左右两侧仍有一个空白区域以及高度问题。我该如何解决这个问题?

自定义操作栏

<?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:background="#3A86CF"
        android:orientation="horizontal"
        android:layout_gravity="fill_horizontal">

        <ImageView
            android:id="@+id/imgLeftMenu"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical|center_horizontal"
            android:layout_weight="1"
            android:src="@drawable/menu_left" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:id="@+id/txtTitle"
            android:gravity="center_vertical|center_horizontal"
            android:text="All Post"
            android:textColor="@android:color/white" />

        <ImageView
            android:id="@+id/imgSearch"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@android:drawable/ic_menu_search" />

        <ImageView
            android:id="@+id/imgAdd"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@android:drawable/ic_menu_add" />

    </LinearLayout>

活动代码

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_post);
        CustomActionBar();

    }

    public void CustomActionBar()
    {
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayOptions( android.support.v7.app.ActionBar.DISPLAY_SHOW_CUSTOM);

        // Do any other config to the action bar
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // set custom view
        View actionBarView = getLayoutInflater().inflate(
                R.layout.custom_action_bar, null);

        View btnMenuLeft= actionBarView.findViewById(R.id.imgLeftMenu);
        btnMenuLeft.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                            }
        });


        View textView_Title= actionBarView.findViewById(R.id.txtTitle);

        View btnMenuShare= actionBarView.findViewById(R.id.imgSearch);
        View btnMenuAdd= actionBarView.findViewById(R.id.imgAdd);
        android.support.v7.app.ActionBar.LayoutParams params = new android.support.v7.app.ActionBar.LayoutParams(
                android.support.v7.app.ActionBar.LayoutParams.MATCH_PARENT,android.support.v7.app.ActionBar.LayoutParams.MATCH_PARENT);
        actionBar.setCustomView(actionBarView, params);

        // Hide the home icon
        actionBar.setIcon(android.R.color.transparent);
        actionBar.setLogo(android.R.color.transparent);
    }

style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:height">5dp</item>
    </style>
</resources>

1 个答案:

答案 0 :(得分:0)

将以下代码放在style.xml中

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <!-- Customize your theme here. -->
</style>



<!-- ActionBar styles -->
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/actionbar_bg</item>
    <item name="background">@color/actionbar_bg</item>
    <item name="android:homeAsUpIndicator">@drawable/ic_backarrow</item>
    <item name="homeAsUpIndicator">@drawable/ic_backarrow</item>
</style>

在清单

中使用“@ style / AppTheme”

自定义布局

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/cal_tvActionbarTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textColor="@color/white"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textStyle="bold" />

在您的java类中为actionbar写下面的代码

/**
 * Method to change ActionBar Title
 */
public void setMainScreenActionBar(String p_title)
{

    LayoutInflater m_inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ActionBar m_actionBar = getSupportActionBar();
    View m_viewActionBar = m_inflater.inflate(R.layout.custom_actionbar_title_layout, null);
    ActionBar.LayoutParams m_params = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
    TextView m_tvTitle = (TextView) m_viewActionBar.findViewById(R.id.cal_tvActionbarTitle);
    m_tvTitle.setText(p_title);
    m_actionBar.setCustomView(m_viewActionBar, m_params);
    m_actionBar.setDisplayShowCustomEnabled(true);
    m_actionBar.setDisplayShowTitleEnabled(false);
    m_actionBar.setDisplayHomeAsUpEnabled(true);
    m_actionBar.setHomeButtonEnabled(true);

}