Android: How to Center title in ToolBar

后端 未结 19 2036
执念已碎
执念已碎 2020-11-27 19:04

I am using ToolBar in my project first time, so i do not know how to customized the toolbar in android. I need to centered title in to the tool bar and how to do that please

19条回答
  •  孤独总比滥情好
    2020-11-27 19:35

    This did my work!

    Toolbar toolbar = findViewById(R.id.mytoolbar);
            TextView titleText = getProperties(new TextView(this));
            FrameLayout frameLayout = new FrameLayout(this);
            frameLayout.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
            frameLayout.addView(titleText);
            toolbar.addView(frameLayout);
            setSupportActionBar(toolbar);
    

    Method to Set TextView Properties

     private TextView getProperties(TextView titleText){
            titleText.setText(wallpaperName);
            titleText.setTextColor(Color.WHITE);
            titleText.setTextSize(18f);
            titleText.setShadowLayer(2f,2f,2f,Color.BLACK);
            titleText.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); // CENTER ALIGNMENT
            return titleText;
        }
    

提交回复
热议问题