English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Introdução ao uso do DrawerLayout do Android

no android support.v4 Há um controle de vista de gaveta DrawerLayout. Usando esse controle, é possível gerar um menu que pode ser aberto ou fechado deslizando horizontalmente na tela, proporcionando uma boa experiência ao usuário.

DrawerLayout é dividido em duas partes: o menu lateral e a área de conteúdo principal. O menu lateral pode ser expandido e ocultado conforme o gesto, e a área de conteúdo principal pode mudar conforme o clique no menu. DrawerLayout é realmente um controle, semelhante ao LinearLayout, e pode ser usado diretamente.

DrawerLayout properties

1、drawerPosition: Specifies that the drawer will slide from one side of the screen.

2、drawerWidth: Specifies the width of the drawer, that is, the exact width from the edge of the window to the view.

3、keyboardDismissMode: Determines whether the keyboard responds to the rejection of the drag.

  • 'none' (default value), the drag does not affect the keyboard.
  • 'on-drag', the drag starts, the keyboard is rejected.

4、onDrawerClose: The function is called when the navigation view is closed.

5、onDrawerOpen: The function is called when the navigation view is opened.

6、onDrawerSlide: The function is called when interacting with the navigation view.

7、onDrawerStateChanged: The function is called when the Drawer state changes, drawer has 3 types: 

  •  idle -- Represents that there is no interaction with the navigation view
  •  dragging -- Represents that there is currently interaction with the navigation view
  •  settling -- Represents that there is interaction with the navigation view and the navigation view is currently closed or open.

8、renderNavigationView: The navigation view will be rendered on one side of the screen and can be pulled out.

Cases

Using imported dependency library

compile 'com.android.support:appcompat-v7:24.2.1' 

Layout file

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@"+id/v4_drawerlayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <FrameLayout
    android:id="@"+id/v4_drawerlayout_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@"+id/v4_text"
      android:textSize="22sp"
      android:textColor="@color/colorAccent"
      android:gravity="center"
      />
  </FrameLayout>
  <ListView
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:id="@"+id/v4_listview"
    android:choiceMode="singleChoice"
    android:background="@android:color/white" />
</android.support.v4.widget.DrawerLayout> 

Activity

public class DrawerActivity extends AppCompatActivity {
  private ListView listView;
  private DrawerLayout drawerLayout;
  private TextView textView;
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawer_activity);
    initView();
  }
  private void initView()
  {
    listView=(ListView) findViewById(R.id.v4_listview);
    drawerLayout=(DrawerLayout) findViewById(R.id.v4_drawerlayout);
    textView=(TextView) findViewById(R.id.v4_text);
    initDate();
  }
  private void initDate(){
    final List<String> list = new ArrayList<String>();
    list.add("网易");
    list.add("腾讯");
    list.add("新浪");
    list.add("搜狐");
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<63;> parent, View view, int position, long id) {
        textView.setText(list.get(position));
        showDrawerLayout();
      }
    });
    drawerLayout.openDrawer(Gravity.LEFT);//Abrir deslize lateral Não configurar não será aberto por padrão
  }
  private void showDrawerLayout() {
    if (!drawerLayout.isDrawerOpen(Gravity.LEFT)) {
      drawerLayout.openDrawer(Gravity.LEFT);
    }
      drawerLayout.closeDrawer(Gravity.LEFT);
    }
  }
} 

O efeito de execução é mostrado na figura:

Endereço de download:Drawerlayout_jb51.rar

Isso é tudo o que há no artigo, espero que ajude no seu aprendizado e que você apoie mais o tutorial de gritaria.

Declaração: O conteúdo deste artigo é extraído da Internet, pertence ao respectivo proprietário, o conteúdo é contribuído e carregado voluntariamente pelos usuários da Internet, este site não possui direitos de propriedade, não foi editado manualmente e não assume responsabilidade legal relevante. Se você encontrar conteúdo suspeito de violação de direitos autorais, por favor, envie um e-mail para: notice#w3Aviso: O conteúdo deste artigo é extraído da Internet, pertence ao respectivo proprietário, o conteúdo é contribuído e carregado voluntariamente pelos usuários da Internet, este site não possui direitos de propriedade, não foi editado manualmente e não assume responsabilidade legal relevante. Se você encontrar conteúdo suspeito de violação de direitos autorais, por favor, envie um e-mail para: notice#w

Você também pode gostar