如何使用Retrofit android设置工具栏标题?

时间:2019-07-03 05:02:08

标签: java android retrofit2 toolbar

我使用了CollapsingToolbarLayout并尝试使用改型来动态设置工具栏标题,但是它不起作用。看看我的代码:-

Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final ApiInterface api = ApiClient.getApiService();
    Call<GetAttributesResponse> call = api.getAttributes(27, attributId);

    call.enqueue(new Callback<GetAttributesResponse>() {
        @SuppressLint("SetTextI18n")
        @Override
        public void onResponse(@NonNull Call<GetAttributesResponse> call, @NonNull Response<GetAttributesResponse> response) {
            if (response.code() == 200) {
                if (response.body() != null) {
                        titleName = response.body().getData().getAttributeName();
                        getSupportActionBar().setTitle(titleName);
                        toolbar.setTitle(titleName);
                    }
                }

        }

我的Xml代码结构:-

<android.support.design.widget.CoordinatorLayout 
<android.support.design.widget.AppBarLayout
    <android.support.design.widget.CollapsingToolbarLayout

         <ImageView
         </ImageView>
         <android.support.v7.widget.Toolbar
         </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

1 个答案:

答案 0 :(得分:0)

尝试一下:

collapsingToolbarLayout.setTitleEnabled(false);
toolbar.setTitle("My Title");

调用setTitleEnabled(false);时,标题会出现在工具栏中