android 开发(一些控件用法)

1. 属性

XML标记内部id语法:

android:id="@+id/button"

+号表示这是一个新的资源名称

引用Android资源Id时不需要加(+),但是需要加android命名空间

android:id="@android:id/button"

 引用创建好的id

1.定义视图/微件,并为其分配唯一id

<Button android:id="@+id/my_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/my_button_text"/>

2.捕获它

Button myButton = (Button) findViewById(R.id.my_button);

2. Toolbar(标题栏)

1.修改标题栏上显示的文字内容(如果需要的话)

在AndroidManifest.xml中添加

<activity
        ...
        android:label="需要改的名字">
</activity>

 在AndroidManifest.xml会看到一行

android:theme = "@style/AppTheme"

这是在res/values/styles.xml中定义的

Toolbar替换ActionBar就要在styles.xml中指定一个不带ActionBar的主题

1.Theme.AppCompat.NoActionBar表示深色主题

2.Theme.AppCompat.Light.NoActionBar表示浅色主题

修改styles.xml文件

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

colorPrimary代表标题栏的颜色

colorPrimaryDark代表屏幕最上方的那一小块区域

colorAccent代表控件选中的颜色

textColorPrimary代表标题栏字体的颜色

navigationBarColor底部颜色

在activity_main.xml中添加

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.appcompat.widget.Toolbar
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:id="@+id/toolabr"
        android:background="#CDB79E"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</FrameLayout>

 build.gradle中添加

implementation 'androidx.drawerlayout:drawerlayout:1.1.1'

.在res目录下创建一个menu文件夹,然后在menu文件下创建一个Toolbar.xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/backup"
        android:icon="@drawable/anniu"
        android:title="Backup"
        app:showAsAction="always"/>

    <item
        android:id="@+id/delete"
        android:icon="@drawable/anniu"
        android:title="Delete"
        app:showAsAction="ifRoom"/>
    <item
        android:id="@+id/settings"
        android:icon="@drawable/anniu"
        android:title="Settings"
        app:showAsAction="never"/>

</menu>

用<item>标签定义action按钮

android:id用于指定按钮的id

android:icon用于指定按钮的图标

android:title用于指定按钮的文字

app:showAsAction来指定按钮的显示位置

1. always表示永远显示在Tolbar中,屏幕不够就不显示

2. ifRoom表示屏幕空间足够的情况下显示在Toolbar中,不够的话显在菜单中

3. action永远显示在菜单中

在Toolbar肿的action是只会显示图标, 菜单中的action只会显示文字

修改MainActivity中代码

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;


public class ToolbarActivity extends AppCompatActivity {

    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toolbar);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolabr);
        setSupportActionBar(toolbar);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case R.id.backup:
                Toast.makeText(this, "You clicked Backup", Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                Toast.makeText(this, "You clicked Delete", Toast.LENGTH_SHORT).show();
                break;
            case R.id.settings:
                Toast.makeText(this, "You clicked Settings", Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }
}

3. DrawerLayout(滑动菜单)

修改acticity_main.xml

<?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"
    tools:context=".ToolbarActivity"
    android:id="@+id/drawer_layout">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <androidx.appcompat.widget.Toolbar
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                android:id="@+id/toolabr"
                android:background="#CDB79E"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    </FrameLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:text="This is menu"
        android:textSize="30sp"
        android:background="#fff"/>

</androidx.drawerlayout.widget.DrawerLayout>

layout_gravity属性是指定菜单的左右,start是根据系统语言进行判断,比如英语\汉语就在左边,阿拉伯语就是在右边

准备一张菜单按钮图片,把他放在res--drawable-xhdpi文件夹里面

修改MainActivity中代码

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;


public class ToolbarActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;

    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toolbar);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolabr);
        setSupportActionBar(toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.anniu);
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                break;
            case R.id.backup:
                Toast.makeText(this, "You clicked Backup", Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                Toast.makeText(this, "You clicked Delete", Toast.LENGTH_SHORT).show();
                break;
            case R.id.settings:
                Toast.makeText(this, "You clicked Settings", Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }
}
openDrawer()方法传入的参数与xml中一致

 4. NavigationView(好看一点的滑动菜单)

添加依赖

implementation 'com.google.android.material:material:1.7.0'
implementation 'de.hdodenhof:circleimageview:3.0.1'

 新建nav_menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
    <item
    android:id="@+id/nav_call"
    android:icon="@drawable/shaojiu"
    android:title="Call"
    android:layout_width="100dp"/>
    <item
        android:id="@+id/nav_luobo"
        android:icon="@drawable/luobo"
        android:title="Call"
        android:layout_width="100dp"/>
    <item
        android:id="@+id/nav_maodou"
        android:icon="@drawable/maodou"
        android:title="Call"
        android:layout_width="100dp"/>
    <item
        android:id="@+id/nav_doufu"
        android:icon="@drawable/doufu"
        android:title="Call"
        android:layout_width="100dp"/>


</group>
</menu>

新建nav_header文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:padding="10dp"
    android:background="?attr/colorPrimary">
    <de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/shaojiu"
        android:layout_centerInParent="true"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/username"
        android:layout_alignParentBottom="true"
        android:text="tonygreendev@gmail.com"
        android:textColor="#fff"
        android:textSize="14sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mail"
        android:layout_above="@id/username"
        android:text="Tony Green"
        android:textColor="#fff"
        android:textSize="14sp"/>

</RelativeLayout>

 修改xml文件

<?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"
    tools:context=".ToolbarActivity"
    android:id="@+id/drawer_layout">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <androidx.appcompat.widget.Toolbar
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                android:id="@+id/toolabr"
                android:background="#CDB79E"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    </FrameLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/nav_menu"
        app:headerLayout="@layout/nav_header"
    />
 <!--   <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:text="This is menu"
        android:textSize="30sp"
        android:background="#fff"/>-->

</androidx.drawerlayout.widget.DrawerLayout>

修改java文件

onCreate()方法里添加

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

navigationView.setCheckedItem(R.id.nav_call);
navigationView.setNavigationItemSelectedListener(item -> {
    drawerLayout.closeDrawer(GravityCompat.START);
    return true;
});

 5. FloatingActionButton(悬浮按钮)

添加依赖

implementation 'com.getbase:floatingactionbutton:1.10.1'

 修改xml文件

<?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"
    tools:context=".ToolbarActivity"
    android:id="@+id/drawer_layout">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
                <androidx.appcompat.widget.Toolbar
                    android:layout_height="?attr/actionBarSize"
                    android:layout_width="match_parent"
                    android:id="@+id/toolabr"
                    android:background="#CDB79E"
                    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="16dp"
            app:elevation="8dp"/>

    </FrameLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/nav_menu"
        app:headerLayout="@layout/nav_header"
    />
 <!--   <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:text="This is menu"
        android:textSize="30sp"
        android:background="#fff"/>-->
</androidx.drawerlayout.widget.DrawerLayout>

 修改java文件

onCreate()方法里添加

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(this);

实现

implements View.OnClickListener
@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.fab:
            Snackbar.make(view, "Data delete", Snackbar.LENGTH_SHORT)
                    .setAction("Undo", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(ToolbarActivity.this, "Data restored", Toast.LENGTH_SHORT).show();
                        }
                    }).show();  //使用的Snackbar控件
           /* Toast.makeText(ToolbarActivity.this, "FAB clicked", Toast.LENGTH_SHORT).show();*/
            break;
    }
}

 6. Sncakbar(交互按钮)

将java中onCreate()方法中的toast换成Sncakbar形式

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fab:
                Snackbar.make(view, "Data delete", Snackbar.LENGTH_SHORT)
                        .setAction("Undo", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Toast.makeText(ToolbarActivity.this, "Data restored", Toast.LENGTH_SHORT).show();
                            }
                        }).show();
               /* Toast.makeText(ToolbarActivity.this, "FAB clicked", Toast.LENGTH_SHORT).show();*/
                break;
        }
    }

 点击后发现弹框会将悬浮按钮挡住,,,,可以使用以下控件

7. CoordinatorLayout(加强版的FrameLayout)

implementation 'androidx.cardview:cardview:1.0.0'

将xml文件中FrameLayout修改为

androidx.coordinatorlayout.widget.CoordinatorLayout

8.CardView(卡片布局)

    implementation 'androidx.cardview:cardview:1.0.0'

还需要用到

implementation 'androidx.drawerlayout:drawerlayout:1.1.1'

 在xml中CoordinatorLayout中加入

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

新建实体类Fruit

public class Fruit {

    private String name;

    private int imageId;

    public Fruit(String name, int imageId){
        this.name = name;
        this.imageId = imageId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }
}

 为RecyclerView定义一个布局,创建一个xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
    android:layout_margin="5dp"
    app:cardCornerRadius="4dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/fruit_image_view"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:scaleType="centerCrop"/>

        <TextView
            android:id="@+id/fruit_name_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_margin="5dp"
            android:textSize="16sp"/>
    </LinearLayout>
</androidx.cardview.widget.CardView>

上面scaleType是为了让图片都能填充整个imageView,可以让图片按原比例填充,并将超出屏幕的部分减掉

 为RecyclerView准备一个配置器

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;


public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

    private List<Fruit> mFruitList;

    private Context mContext;

    static class ViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        ImageView fruitImage;
        TextView fruitName;
        public ViewHolder(View view) {
            super(view);
            cardView = (CardView) view;
            fruitImage = (ImageView) view.findViewById(R.id.fruit_image_view);
            fruitName = (TextView) view.findViewById(R.id.fruit_name_view);
        }
    }

    public RecyclerAdapter(List<Fruit> fruitList) {
        mFruitList = fruitList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.fruit_item_cardview, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder,int position) {
        Fruit fruit = mFruitList.get(position);
        holder.fruitName.setText(fruit.getName());
        holder.fruitImage.setImageResource(fruit.getImageId());
    }

    @Override
    public int getItemCount () {
        return mFruitList.size();
    }
}

修改java中代码

 private Fruit[] fruits = {new Fruit("毛豆", R.drawable.maodou),new Fruit("毛豆", R.drawable.maodou),
            new Fruit("毛豆", R.drawable.maodou),new Fruit("毛豆", R.drawable.maodou),
            new Fruit("毛豆", R.drawable.maodou),new Fruit("毛豆", R.drawable.maodou),
            new Fruit("毛豆", R.drawable.maodou),new Fruit("毛豆", R.drawable.maodou)};

    private List<Fruit> fruitList = new ArrayList<>();

    private RecyclerAdapter adapter;

 在onCreate中添加

 initFruits();
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new RecyclerAdapter(fruitList);
        recyclerView.setAdapter(adapter);

新建一个方法

 private void initFruits(){
        fruitList.clear();
        for (int i = 0; i < 50; i++){
            Random random = new Random();
            int index = random.nextInt(fruits.length);
            fruitList.add(fruits[index]);
        }
    }

9.AppBarLayout(实现RecyclerView在Toolbar空间下面)

在xml中CoordinatorLayout下面给toolbar外面套一个AppBarLayout

   <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.appcompat.widget.Toolbar
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent"
            android:id="@+id/toolabr"
            android:background="#CDB79E"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            />
        <!-- app:navigationIcon="@drawable/shouqianniu"
         android:navigationIcon="@drawable/shouqianniu"-->
    </com.google.android.material.appbar.AppBarLayout>

在RecyclrView中添加app:layout_behavior="@string/appbar_scrolling_view_behavior" 

 <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" />

在toolbar中添加app:layout_scrollFlags="scroll|enterAlways|snap"

  <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.appcompat.widget.Toolbar
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent"
            android:id="@+id/toolabr"
            android:background="#CDB79E"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            />
        <!-- app:navigationIcon="@drawable/shouqianniu"
         android:navigationIcon="@drawable/shouqianniu"-->
    </com.google.android.material.appbar.AppBarLayout>

可实现向上混动时隐藏toolbar向下滚动出现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值