代码之家  ›  专栏  ›  技术社区  ›  Nikola C

如何根据文本长度以编程方式更改工具栏高度和字体大小

  •  1
  • Nikola C  · 技术社区  · 7 年前

    我有很多不同的头衔 Toolbar

    title = getIntent().getStringExtra("title");
    

    我将它们设置到我的新活动工具栏上,如下所示:

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setTitle(title);
    

    但字体太大,只覆盖工具栏的一行: enter image description here

    雷达距离传播公里转换为微秒时间,反之亦然

    我想知道有没有一种动态变化的方法 高度和文本大小是否适合此标题?这可以通过编程实现吗?

    谢谢

    2 回复  |  直到 7 年前
        1
  •  3
  •   AskNilesh    7 年前

     <android.support.v7.widget.Toolbar
         android:id="@+id/toolbar"
         android:layout_width="match_parent"
         android:layout_height="?attr/actionBarSize"
         android:background="?attr/colorPrimary"
         app:popupTheme="@style/AppTheme.PopupOverlay">
    
            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Did you know"
                android:id="@+id/myTitle"
                android:textColor="@android:color/white" />
        </LinearLayout>
    </android.support.v7.widget.Toolbar>
    

    并将活动主题用作NoActionBar

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    TextView myTitle = (TextView) toolbar.findViewById(R.id.myTitle);
    
        2
  •  1
  •   Vishal Vaishnav    7 年前

    您可以这样尝试:

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/action_bar_bkgnd"
    app:theme="@style/ToolBarTheme" >
    
    
     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toolbar Title"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title" />
    
    
    </android.support.v7.widget.Toolbar>
    

    和java文件;

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_top);
    TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);