Pular para o conteúdo
CrossFly é 100% open sourceSiga no GitHub
CrossFlyCrossFly
Começar
Todos os exemplos
ReceitasNavegaçãoLayout65 linhas · 1 arquivos

Bottom nav that becomes a rail

One flag — tabsAtBottom — and the strip is a bottom bar on a phone and a rail on a tablet. Resize the window to see it happen.

O que isto mostra

Bottom navigation that turns into a rail on a wide screen, with no width check anywhere.

Resize the window and watch the strip move to the left edge and grow labels. Nothing below asks how wide the screen is, listens for an orientation change, or holds a phone/tablet flag — that behaviour is the TabBar's, and an app that reimplements it gets a second, slightly different answer to the same question.

Rodar na sua máquina

Todo exemplo é uma Window comum, então o launcher só precisa do nome dela.

gradlew lwjgl3:run --args="classname dev.cf.recipe.R12Nav"
Editar o exemplo

Java é compilado antes de rodar, então o navegador não consegue reconstruir uma edição sozinho — o código que você vê aqui virou JavaScript pelo TeaVM antes de ser publicado. Copie o arquivo para um projeto e ele roda sem mudanças nas quatro plataformas; o comando acima é toda a preparação.

Rodando aquiCrossFly 0.1.32
Iniciando o aplicativo…

Compilado para JavaScript pelo TeaVM — é o mesmo código, não um vídeo dele. · Todos os exemplos

O que ele usa

Tipos do framework que este exemplo toca. Os que têm link têm página de referência.

ComponentMaterialIconSkinStylesTabBarVerticalLayoutWidgets

O código

Completo, sem cortes. É o arquivo, não um trecho dele.

Bruto
dev/cf/recipe/R12Nav.java
package dev.cf.recipe;

import dev.crossfly.ui.*;
import dev.crossfly.ui.graphics.*;
import dev.crossfly.ui.layout.*;
import dev.crossfly.ui.skin.Skin;
import dev.crossfly.ui.style.*;

import static dev.crossfly.ui.Widgets.*;

/**
 * Bottom navigation that turns into a rail on a wide screen, with no width check anywhere.
 *
 * <p>Resize the window and watch the strip move to the left edge and grow labels. Nothing below
 * asks how wide the screen is, listens for an orientation change, or holds a phone/tablet flag —
 * that behaviour is the {@link TabBar}'s, and an app that reimplements it gets a second, slightly
 * different answer to the same question.</p>
 */
public class R12Nav extends Recipe
{
   private static final int IC_HOME = 0xE88A, IC_LIST = 0xE8EF, IC_CHART = 0xE6E1, IC_MORE = 0xE5D2;

   public String title()
   {
      return "Bottom nav that becomes a rail";
   }

   public String note()
   {
      return "One flag — tabsAtBottom — and the strip is a bottom bar on a phone and a rail on a"
         + " tablet. Resize the window to see it happen.";
   }

   public void build(Component body) throws Exception
   {
      TabBar tabs = new TabBar(new TabBar.Tab("Home", MaterialIcon.of(IC_HOME, 1.5f)),
         new TabBar.Tab("Items", MaterialIcon.of(IC_LIST, 1.5f)),
         new TabBar.Tab("Reports", MaterialIcon.of(IC_CHART, 1.5f)),
         // railHidden: a "More" tab exists to reach what does not fit on a phone. On a rail
         // everything already fits, so the tab that only reopens the same list is dropped.
         new TabBar.Tab("More", MaterialIcon.of(IC_MORE, 1.5f)).railHidden(true));

      tabs.setContent(page("Home"), page("Items"), page("Reports"), page("More"));
      // A badge is a count the user has not dealt with. Null clears it — never show a zero.
      tabs.badge(1, "3");
      tabs.style(Skin.get()
         .tabBar()
         .copy()
         .tabsAtBottom(true));
      tabs.testTag("recipe.nav");
      body.add(LAY_FILL, tabs);
   }

   /** Each destination is a Component the bar keeps alive — not a Window. See dev.cf.app.crud. */
   private Component page(String name) throws Exception
   {
      Component c = new Component();
      c.setLayout(new VerticalLayout(Position.CENTER, Position.CENTER));
      c.add(LAY_AUTO, label(name, 1.5f, true, Styles.textColor, Position.CENTER));
      c.add(LAY_AUTO, outgap(label("Kept alive across switches, so coming back finds it where you"
         + " left it.", 1f, false, Styles.captionColor, Position.CENTER), 1f, .4f, 1f, 0));
      return c;
   }
}
Para uma IA

Versões desta página legíveis por máquina, para um modelo ler a prateleira em vez de adivinhar a API.

Este exemplo, em JSONEste exemplo, em Markdown
AnteriorTelling the user somethingPróximoWaiting