Android Studio 2 - 9 ActionBar和DrawLayout和SlidingMenu
DrawerLayout
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--android:layout_gravity="left"-->
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/dDrawerLayout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4CAF50"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="8"
android:background="#4CAF50"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:background="#2196F3"></LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#FF9800"
android:orientation="vertical">
<Button
android:id="@+id/btn_closeD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭"
android:textSize="25sp" />
<Button
android:id="@+id/btn_toastD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="吐司"
android:textSize="25sp" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
</LinearLayout>
代码
package com.example.day09drawer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView iv_image;
private Button btn_closeD;
private Button btn_toastD;
private DrawerLayout dDrawerLayout_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
iv_image = (ImageView) findViewById(R.id.iv_image);
btn_closeD = (Button) findViewById(R.id.btn_closeD);
btn_toastD = (Button) findViewById(R.id.btn_toastD);
dDrawerLayout_main = (DrawerLayout) findViewById(R.id.dDrawerLayout_main);
btn_closeD.setOnClickListener(this);
btn_toastD.setOnClickListener(this);
iv_image.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_closeD:
dDrawerLayout_main.closeDrawer(Gravity.LEFT);
break;
case R.id.btn_toastD:
Toast.makeText(this, "么么达", Toast.LENGTH_SHORT).show();
break;
case R.id.iv_image:
dDrawerLayout_main.openDrawer(Gravity.LEFT);
break;
default:
}
}
}
SlidingMenu
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4CAF50"
tools:context=".SlideActivity">
<ImageView
android:id="@+id/iv_SlideImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
代码
package com.example.day09drawer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
public class SlideActivity extends AppCompatActivity {
private SlidingMenu slidingMenu;
private ImageView iv_SlideImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slide);
initView();
initMenu();
}
private void initView() {
iv_SlideImage = (ImageView) findViewById(R.id.iv_SlideImage);
iv_SlideImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
slidingMenu.showMenu();
}
});
}
private void initMenu() {
slidingMenu = new SlidingMenu(this);
slidingMenu.setMode(SlidingMenu.LEFT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setBehindOffset(200);
slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
View view = LayoutInflater.from(this).inflate(R.layout.slide, null);
Button btn_slide_close = view.findViewById(R.id.btn_slide_close);
Button btn_slide_Toast = view.findViewById(R.id.btn_slide_Toast);
btn_slide_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
slidingMenu.showContent();
}
});
slidingMenu.setMenu(view);
slidingMenu.setOnOpenedListener(new SlidingMenu.OnOpenedListener() {
@Override
public void onOpened() {
}
});
slidingMenu.setOnClosedListener(new SlidingMenu.OnClosedListener() {
@Override
public void onClosed() {
}
});
}
}
ToolBar
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ToolBActivity">
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/dDrawerLayout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4CAF50"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/tToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"></androidx.appcompat.widget.Toolbar>
<ImageView
android:id="@+id/iv_tool_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_tool"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本"
android:textColor="#FFEB3B"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="8"
android:background="#4CAF50"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:background="#2196F3"></LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#FF9800"
android:orientation="vertical">
<Button
android:id="@+id/btn_tool_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭"
android:textSize="25sp" />
<Button
android:id="@+id/btn_tool_Toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="吐司"
android:textSize="25sp" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
</LinearLayout>
代码
tToolbar = (Toolbar) findViewById(R.id.tToolbar);
tToolbar.inflateMenu(R.menu.iitem);
tToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.one:
Toast.makeText(ToolBActivity.this, "1", Toast.LENGTH_SHORT).show();
break;
case R.id.two:
Toast.makeText(ToolBActivity.this, "22", Toast.LENGTH_SHORT).show();
break;
case R.id.three:
Toast.makeText(ToolBActivity.this, "333", Toast.LENGTH_SHORT).show();
break;
default:
}
return true;
}
});
slidingMenu.showContent();