web-dev-qa-db-ja.com

タブレイアウトでタブ幅を設定する方法は?

2つのtabsを持つtab layoutを作成しようとしています。小さなモバイルでアプリを実行すると、tab layoutは問題なく見えますが、Tabletで同じアプリケーションを実行すると、このように表示されます。

タブレットでは次のようになります。

enter image description here

各タブが両端に隙間なくスペース全体を占めるようにします。このようにtab layoutを作成しました...

private void drawTabLayout() {
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.addTab(tabLayout.newTab().setText("Imprint") );
    tabLayout.addTab(tabLayout.newTab().setText("Legal"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setTabMode(TabLayout.MODE_FIXED);
    tabLayout.setSelectedTabIndicatorHeight(10);
    tabLayout.setBackgroundColor(Color.WHITE);
}

多くのサイトを検索しましたが、正しい回答が見つかりませんでした。

16
user5912784

このコードをtab_layout.xmlに追加します

<Android.support.design.widget.TabLayout
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            app:tabMaxWidth="0dp"
            app:tabGravity="fill"
            app:tabMode="fixed" />

それがあなたのために働くことを願っています...

28
HassanUsman

各タブにカスタムビューがあるタブレイアウトを作成し、そのタブを任意のサイズでtablayoutに追加できます

View customView = LayoutInflater.from(getContext()).inflate(R.layout.tab_text, null); 

addTab(newTab().setCustomView(customView))
1
Rohit