Android其它常用控件(补)

48 阅读3分钟

image.png

别人怎么看待你,只是阶段性的,你怎么看待自己,这才是一生的命运。

工具栏(ToolBar)

Android Studio 中默认创建 1 个 App 时,顶部都会默认添加导航栏 ActionBar 组件。但是 ActionBar 存在一些缺点,如使用不灵活难以扩展等问题。因此在 Android 5.0 时 ToolBar 诞生了, 由于它高度的可定制性、灵活性、具有 Material Design 风格等优点,越来越多的 App 开始使用 ToolBar。 为了兼容以前的系统版本,ActionBar 仍然需要保留,但是要想使用 ToolBar 就需要先将 ActionBar 关闭。

在该文件下关闭:

image.png

关闭前:.

image.png

关闭后:

image.png

应用布局:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/MyToolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#4FE6E6"
    tools:ignore="MissingConstraints">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToolBar"/>

</androidx.appcompat.widget.Toolbar>

java代码设置:

private void initView() {
//        设置左侧导航图标
        toolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_back_24);
//        设置工具栏图标
        toolbar.setLogo(R.mipmap.ic_launcher);
//        监听左侧图标点击事件
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(MainActivity.this,MainActivity2.class);
                startActivity(intent);
            }
        });

    }

效果展示:

image.png

折叠标题栏(CollapsingToolbarLayout)

CollapsingToolbarLayout 用于美化 Toolbar 的 运 行 效 果, 它 是 由 Design Support 库 提 供 的。 CollapsingToolbarLayout 被设计用于 AppBarLayout 的子布局,AppBarLayout 是一个垂直的 LinearLayout, 只是为了实现交互动画效果它增加了一些滚动特性。AppBarLayout 只有作为 CoordinatorLayout 的直接 子布局才可以正常的工作,如果 CoordinatorLayout 包含了一个不同的 ViewGroup,滚动功能将无法实现。

布局实现:

<?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=".MainActivity2"
    android:background="#EC8686">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:fitsSystemWindows="true"
        tools:ignore="MissingConstraints">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingToolberLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="38dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/main_backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@mipmap/img"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7"/>

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolber"
                android:fitsSystemWindows="true"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                app:layout_collapseMode="parallax" />
        </com.google.android.material.appbar.CollapsingToolbarLayout>


    </com.google.android.material.appbar.AppBarLayout>

//增加这个是为了实现滚动效果更好的展示功能

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:ignore="MissingConstraints">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/text"
            android:textSize="23sp"/>
    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

java设置:

 private void initView() {
        toolbar = findViewById(R.id.toolber);
        setSupportActionBar(toolbar);
//        main_backdrop=findViewById(R.id.main_backdrop);
        collapsingToolbarLayout = findViewById(R.id.collapsingToolberLayout);


//        获取折叠工具栏布局
        collapsingToolbarLayout.setTitle("Android 200例-01");//设置标题
        collapsingToolbarLayout.setExpandedTitleColor(Color.RED);//设置标题文本初始颜色;
        collapsingToolbarLayout.setCollapsedTitleTextColor(Color.WHITE);//设置标题文本改变后的颜色
        collapsingToolbarLayout.setContentScrimColor(getResources().getColor(R.color.teal_700));//设置内容改变后的背景颜色

    }

效果展示:

tutieshi_322x616_11s.gif

卡片式布局(CardView)

CardView 是 FrameLayout 的子类,只是单独提供了圆角与阴影的效果,看起 来立体感更强、更加美观。CardView 一般被使用在如 ListView、GridView、RecyclerView 等列表视 图的子项(item)布局中。

布局设置:

<androidx.cardview.widget.CardView
    android:id="@+id/cardView"
    android:background="#B3B2B2"
    android:layout_margin="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/teal_200"
    tools:ignore="MissingConstraints">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:scaleType="centerCrop"
            android:src="@mipmap/img"/>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="Android控件"
                android:textColor="#000"
                android:textSize="16sp"
                android:textStyle="bold"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="想学就要多练"
                android:textColor="#000"
                android:textSize="14sp"/>
        </LinearLayout>
    </LinearLayout>
</androidx.cardview.widget.CardView>

具体代码设置:

private void initView() {
        cardView=findViewById(R.id.cardView);

        cardView.setRadius(15);//设置卡片视图圆角半径
        cardView.setCardElevation(20);//设置阴影部分大小
        cardView.setContentPadding(5,5,5,5);//设置卡片视图与内容的边距

//        点击监听
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity3.this, "点击了", Toast.LENGTH_SHORT).show();
            }
        });
    }

效果展示:

image.png

滑动菜单(DrawerLayout)

DrawerLayout 在 Android v4 包现在Androidx整合了v4,v7包,androidX可以直接实现侧滑菜单效果的布局,它显示在屏幕的最左侧,默认 情况下是隐藏的,当用户用手指从屏幕左侧向右侧滑动时该组件将显示,单击该布局外侧或者向原 来的方向滑动时该组件消失。

布局设置:

<?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=".MyDrawerLayout">

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="我是主屏幕" />
        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/teal_200"
            android:orientation="vertical">
            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/listView"/>
        </LinearLayout>
    </androidx.drawerlayout.widget.DrawerLayout>

</LinearLayout>

交互设置:

package com.example.permissionapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;

import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MyDrawerLayout extends AppCompatActivity {
    private DrawerLayout drawerLayout;
    private ListView listView;

    private String[] item=new String[]{"设置","网络","飞行模式","系统更新"};




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_drawer_layout);

        initView();
    }

    private void initView() {

        listView=findViewById(R.id.listView);
        ArrayAdapter adapter=new ArrayAdapter(this, android.R.layout.simple_list_item_1,item);
        listView.setAdapter(adapter);

        drawerLayout=findViewById(R.id.drawerLayout);

        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override//发生变化时调用
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

            }

            @Override//完全处于打开状态调用
            public void onDrawerOpened(@NonNull View drawerView) {
                Toast.makeText(MyDrawerLayout.this, "打开", Toast.LENGTH_SHORT).show();
            }

            @Override//完全关闭状态调用
            public void onDrawerClosed(@NonNull View drawerView) {
                Toast.makeText(MyDrawerLayout.this, "关闭", Toast.LENGTH_SHORT).show();
            }

            @Override//滑动状态改变时调用;状态值STATE_IDLE:闲置、STATE_DRAGGING:拖拽、STATE_SETTLING:固定的
            public void onDrawerStateChanged(int newState) {

            }
        });
    }
}

人间的事,只要生机不灭,即使重遭天灾人祸,暂被阻抑,终有抬头的日子。