Saltar al contenido
CrossFly es 100% open sourceSíguenos en GitHub
CrossFlyCrossFly
Empezar
Todos los ejemplos
RecetasNavegaciónDisposición65 líneas · 1 archivos

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.

Qué muestra esto

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.

Ejecutarlo en tu máquina

Cada ejemplo es una Window normal, así que el lanzador solo necesita su nombre.

gradlew lwjgl3:run --args="classname dev.cf.recipe.R12Nav"
Editarlo

Java se compila antes de ejecutarse, así que el navegador no puede reconstruir una edición por su cuenta: el código que ves aquí se convirtió en JavaScript con TeaVM antes de publicarse. Copia el archivo a un proyecto y funciona sin cambios en las cuatro plataformas; el comando de arriba es toda la preparación.

Ejecutándose aquíCrossFly 0.1.32
Iniciando la aplicación…

Compilado a JavaScript por TeaVM: es el mismo código, no un vídeo de él. · Todos los ejemplos

Qué usa

Tipos del framework que toca este ejemplo. Los enlazados tienen página de referencia.

ComponentMaterialIconSkinStylesTabBarVerticalLayoutWidgets

El código

Completo, sin recortes. Es el archivo, no un fragmento.

Crudo
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 una IA

Versiones de esta página legibles por máquina, para que un modelo lea el estante en vez de adivinar la API.

Este ejemplo, en JSONEste ejemplo, en Markdown
AnteriorTelling the user somethingSiguienteWaiting