day012EventBus+DrawerLayout+TabLayout
程序员文章站
2022-03-10 13:06:00
EventBus+DrawerLayout+TabLayout1.EventBus2.约束控件:3.drawLayout4.TabLayout5.FloatingActionButton1.EventBus依赖: implementation 'org.greenrobot:eventbus:3.2.0'actiity:package com.zcb.day012;import androidx.appcompat.app.AppCompatActivity;import android....
EventBus+DrawerLayout+TabLayout
1.EventBus
依赖: implementation 'org.greenrobot:eventbus:3.2.0'
actiity:
package com.zcb.day012;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);
}
@Subscribe
public void getString(String s){
Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
fragment:
package com.zcb.day012;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.fragment.app.Fragment;
import org.greenrobot.eventbus.EventBus;
public class BlankFragment extends Fragment {
private Button btn1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_blank, container, false);
btn1 = (Button) inflate.findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = "哈哈";
EventBus.getDefault().post(s);
}
});
return inflate;
}
}
2.约束控件:
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.1.0'
implementation 'com.google.android.material:material:1.2.1'
<?xml version="1.0" encoding="utf-8"?>
<!--协调者布局-->
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".Main2Activity">
<!-- 通过协调者布局,取到滑动事件
它可以让你定制当某个可滚动View的滚动手势发生变化时,其内部的子View实现何种动作-->
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--可折叠布局 可以单做帧布局来使用
scroll|exitUntilCollapsed 与minHeight 结合使用 效果最常用
app:contentScrim="@color/colorAccent" 蒙版效果-->
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:minHeight="50dp"
app:collapsedTitleGravity="left"
app:expandedTitleGravity="bottom|right"
app:contentScrim="@color/colorPrimaryDark"
app:layout_scrollFlags= "scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="@mipmap/qq"
android:scaleType="centerCrop"></ImageView>
<!-- 折叠以后留下的barlayout_collapseMode 吸附效果-->
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="50dp"
app:title="标题"
android:id="@+id/bar"
app:layout_collapseMode="pin"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<!-- ScrollView的替代品 layout_behavior属性为固定写法-> 点进去看看 -->
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#00BCD4" />
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="@mipmap/a88" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
!-- 圆形风格按钮-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/app_bar"
android:src="@mipmap/ic_launcher"
app:layout_anchorGravity="bottom|right"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
implementation "androidx.drawerlayout:drawerlayout:1.1.1"
3.drawLayout
布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/draw"
tools:context=".Main5Activity">
<LinearLayout
android:background="#2196F3"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn1"
android:text="于谦说:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
<LinearLayout
android:background="#FFC107"
android:layout_gravity="left"
android:id="@+id/left_layout"
android:orientation="horizontal"
android:layout_width="300dp"
android:layout_height="match_parent">
<TextView
android:text="什么乱七八糟的"
android:textColor="#fff"
android:textSize="35dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
代码
package com.zcb.day012;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
public class Main5Activity extends AppCompatActivity {
private DrawerLayout draw;
private Button btn1;
private LinearLayout leftLayout;
private static final String TAG = "Main5Activity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
initView();
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
draw.openDrawer(Gravity.LEFT);
}
});
draw.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
Log.i("zcb", "onDrawerSlide: ");
}
@Override
public void onDrawerOpened(@NonNull View drawerView) {
Log.i("zcb", "onDrawerOpened: ");
}
@Override
public void onDrawerClosed(@NonNull View drawerView) {
Log.i("zcb", "onDrawerClosed: ");
}
@Override
public void onDrawerStateChanged(int newState) {
Log.i("zcb", "onDrawerStateChanged: ");
}
});
}
private void initView() {
draw = (DrawerLayout) findViewById(R.id.draw);
btn1 = (Button) findViewById(R.id.btn1);
leftLayout = (LinearLayout) findViewById(R.id.left_layout);
}
}
4.TabLayout
mytab.addTab(mytab.newTab().setText("选项1").setIcon(R.mipmap.ic_launcher));
mytab.addTab(mytab.newTab().setText("选项2").setIcon(R.mipmap.ic_launcher));
mytab.addTab(mytab.newTab().setText("选项3").setIcon(R.mipmap.ic_launcher));
mytab.addTab(mytab.newTab().setText("选项4").setIcon(R.mipmap.ic_launcher));
mytab.addTab(mytab.newTab().setText("选项5").setIcon(R.mipmap.ic_launcher));
布局文件
<?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=".Main6Activity">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="UserName"></com.google.android.material.textfield.TextInputEditText>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.tabs.TabLayout
android:id="@+id/mytab"
android:layout_width="match_parent"
android:layout_height="50dp"></com.google.android.material.tabs.TabLayout>
</LinearLayout>
5.FloatingActionButton
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/contentView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/anchorView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/anchorView"
app:layout_anchorGravity="bottom|right"
android:onClick="onClick"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"/>
</android.support.design.widget.CoordinatorLayout>
本文地址:https://blog.csdn.net/AeEcho716/article/details/114319672
下一篇: Pandas 如何筛选包含特定字符的列