Removing Extra Space in Custom ActionBar

巧了我就是萌 提交于 2019-12-05 18:17:34

Please try below for removing action bar content by creating base activity so you can refer in all your app & inflate custom action bar from layout

    @SuppressLint("InlinedApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
            getSupportActionBar().hide();
            ActionBar actionBar = getSupportActionBar();
            actionBar.setDisplayHomeAsUpEnabled(false);
            actionBar.setDisplayShowCustomEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayShowHomeEnabled(false);
            actionBar.setHomeButtonEnabled(false);

            actionBar.setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
            View homeIcon = findViewById(android.R.id.home);
            // Hides the View (and so the icon)
            if (homeIcon != null)
                ((View) homeIcon.getParent()).setVisibility(View.GONE);

            overridePendingTransition(0, 0);
    }

add custom view

@SuppressLint("InlinedApi")
@Override
public void setContentView(int layoutResID) 
{
    super.setContentView(R.layout.activity_base);
    ViewGroup viewGroup = (ViewGroup) findViewById(R.id.container);
    viewGroup.removeAllViews();
    viewGroup.addView(getLayoutInflater().inflate(layoutResID, null));
    // you can find action_bar layouts view & add listner
}

and customise XML containing custom action bar in activity_base.xml

<?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="match_parent"
    android:layout_gravity="right"
    android:orientation="vertical" >

    <include layout="@layout/action_bar" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right" />

</LinearLayout>

Where you want to use then extend this activity.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!