2.代码示例:
(1)布局代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 第一个子view会作为内容显示区-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="这里显示你所需要显示的内容"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<!-- 第二个子view会作为侧滑菜单-->
<LinearLayout
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@color/white">
<ListView
android:id="@+id/list_view"
android:layout_width="150dp"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="200dp"
android:layout_gravity="right"
android:background="@color/white"
android:layout_height="match_parent">
<TextView
android:text="用户信息"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
<Button
android:text="退出登录"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
(2)Java代码:
package com.example.test_1;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_view);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
new String[]{"菜单1", "菜单2", "菜单3", "菜单4"});
listView.setAdapter(arrayAdapter);
}
}
(3)图片: