下面将详细介绍各个布局的使用方法,并提供相关的XML和Java代码示例。
1. NavigationView(导航视图)
XML:
xml复制代码
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Main content --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent"/> <!-- Navigation View --> <com.google.android.material.navigation.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:menu="@menu/nav_menu"/> </androidx.drawerlayout.widget.DrawerLayout>
Java:
java复制代码
DrawerLayout drawerLayout = findViewById(R.id.drawer_layout); NavigationView navigationView = findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); // Handle different menu item clicks if (id == R.id.nav_camera) { // Handle the camera action } // Close the drawer drawerLayout.closeDrawer(GravityCompat.START); return true; } });
2. DrawerLayout(抽屉式布局)
XML:
xml复制代码
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Main content --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent"/> <!-- Drawer content --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp"/> </androidx.drawerlayout.widget.DrawerLayout>
Java:
java复制代码
DrawerLayout drawerLayout = findViewById(R.id.drawer_layout); ListView drawerList = findViewById(R.id.left_drawer); String[] drawerItems = {"Item 1", "Item 2", "Item 3"}; drawerList.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, drawerItems)); drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Handle drawer item click drawerLayout.closeDrawer(GravityCompat.START); } });
3. CoordinatorLayout(协调布局)
XML:
xml复制代码
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.appbar.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/header" app:layout_collapseMode="parallax"/> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin"/> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </androidx.coordinatorlayout.widget.CoordinatorLayout>
Java:
java复制代码
RecyclerView recyclerView = findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(new MyAdapter(myDataset));
4. AppBarLayout(应用栏布局)
XML:
xml复制代码
<com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.appbar.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/header" app:layout_collapseMode="parallax"/> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin"/> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout>
Java:
java复制代码
Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar);
5. FragmentLayout(片段布局)
XML:
xml复制代码
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/>
Java:
java复制代码
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit();
6. GridLayout(网格布局)
XML:
xml复制代码
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="2"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" android:layout_row="0" android:layout_column="0"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" android:layout_row="0" android:layout_column="1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3" android:layout_row="1" android:layout_column="0"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 4" android:layout_row="1" android:layout_column="1"/> </GridLayout>
Java:
java复制代码
GridLayout gridLayout = findViewById(R.id.grid_layout);
7. SlidingPaneLayout(滑动窗格布局)
XML:
xml复制代码
<androidx.slidingpanelayout.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/sliding_pane_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Main Content"/> </LinearLayout> <LinearLayout android:layout_width="300dp" android:layout_height="match_parent" android:orientation="vertical" android:background="#FFBBBBBB"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sliding Pane Content"/> </LinearLayout> </androidx.slidingpanelayout.widget.SlidingPaneLayout>
Java:
java复制代码
SlidingPaneLayout slidingPaneLayout = findViewById(R.id.sliding_pane_layout); slidingPaneLayout.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() { @Override public void onPanelSlide(View panel, float slideOffset) { // Handle the sliding of the pane } @Override public void onPanelOpened(View panel) { // Handle the pane being opened } @Override public void onPanelClosed(View panel) { // Handle the pane being closed } });
8. NestedScrollView(嵌套滚动视图)
XML:
xml复制代码
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Item 1"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Item 2"/> <!-- More items here --> </LinearLayout> </androidx.core.widget.NestedScrollView>
Java:
java复制代码
NestedScrollView nestedScrollView = findViewById(R.id.nested_scroll_view);
9. FlexboxLayout(弹性盒子布局)
XML:
xml复制代码
<com.google.android.flexbox.FlexboxLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" app:flexDirection="row" app:justifyContent="center"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 4"/> </com.google.android.flexbox.FlexboxLayout>
Java:
java复制代码
FlexboxLayout flexboxLayout = findViewById(R.id.flexbox_layout);
10. MotionLayout(动态布局)
XML:
xml复制代码
<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/motion_layout" android:layout_width="match_parent" android:layout_height="match_parent" app:layoutDescription="@xml/motion_scene"> <ImageView android:id="@+id/imageView" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/ic_launcher_foreground"/> </androidx.constraintlayout.motion.widget.MotionLayout>
motion_scene.xml:
xml复制代码
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"> <Transition app:constraintSetStart="@id/start" app:constraintSetEnd="@id/end" app:duration="1000"> <OnSwipe app:touchAnchorId="@id/imageView" app:touchAnchorSide="top" app:dragDirection="dragUp"/> </Transition> <ConstraintSet android:id="@id/start"> <Constraint android:id="@id/imageView" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginStart="30dp" android:layout_marginTop="30dp" motion:layout_constraintLeft_toLeftOf="parent" motion:layout_constraintTop_toTopOf="parent"/> </ConstraintSet> <ConstraintSet android:id="@id/end"> <Constraint android:id="@id/imageView" android:layout_width="200dp" android:layout_height="200dp" android:layout_marginEnd="30dp" android:layout_marginBottom="30dp" motion:layout_constraintRight_toRightOf="parent" motion:layout_constraintBottom_toBottomOf="parent"/> </ConstraintSet> </MotionScene>
Java:
java复制代码
MotionLayout motionLayout = findViewById(R.id.motion_layout);
这些布局和代码示例展示了如何在Android应用中使用不同的布局控件。通过理解这些布局的使用方式,可以创建更加复杂和美观的用户界面。