【Android】自学第二天

99 阅读2分钟

前言

紧接上文,接着学

控件相关

1.ImageView 图片

image.png

缩放类型是最重要的属性 特意强调下

image.png 具体效果查看

<ImageView
    android:layout_width="400dp"
    android:layout_height="600dp"
    android:src="@drawable/ic_baseline_accessibility_24"
    android:adjustViewBounds="true"
    android:scaleType="fitStart"
    android:background="@color/red"
    />

2.ProgressBar 进度条

image.png

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical" >


    <ProgressBar
        android:id="@+id/prg_one"
        android:max="100"
        android:visibility="gone"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"></ProgressBar>
    <Button
        android:id="@+id/btn_prg"
        android:layout_width="100dp"
        android:layout_height="50dp" android:layout_gravity="center_horizontal"></Button>



</LinearLayout>
package com.kowaii.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

    public static final String Tag = "hako";
    private ProgressBar progressBar_one;

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


        progressBar_one = findViewById(R.id.prg_one);

        progressBar_one.setProgress(50);

        Button btn = findViewById(R.id.btn_prg);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(progressBar_one.getVisibility() == View.GONE){
                    progressBar_one.setVisibility(View.VISIBLE);
                }else{
                    progressBar_one.setVisibility(View.GONE);
                }


                progressBar_one.setProgress(100);
            }
        });

    }
}

Notification

image.png

image.png

image.png

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_send"
        android:text="发送通知"
        android:onClick="sendNotification"
        android:layout_width="100dp"
        android:layout_height="50dp"></Button>

    <Button
        android:id="@+id/btn_cancel"
        android:text="取消通知"
        android:onClick="cancelNotification"
        android:layout_width="100dp"
        android:layout_height="50dp"></Button>

  
</LinearLayout>
package com.kowaii.demo1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    public static final String Tag = "hako";
    private ProgressBar progressBar_one;
    private NotificationManager notificationManager;
    private Notification notification;

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


        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        //8.0才新增的channel,注意版本判断
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel("hako","测试通知",NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        Intent intent = new Intent(this,NotificationActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);

        notification = new NotificationCompat.Builder(this,"hako")
                .setContentTitle("官方通知")
                .setContentText("通知具体的文本内容")
                .setColor(Color.parseColor("#ff332b"))
                //注意使用的图片不带RGB的
                .setSmallIcon(R.drawable.ic_baseline_access_time_filled_24)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .build();

    }


    public void sendNotification(View view){
        notificationManager.notify(1,notification);
    }

    public void cancelNotification(View view){
        notificationManager.cancel(1);
    }
}
package com.kowaii.demo1;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;

public class NotificationActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.e("hako","进入notificationActivity");
    }
}

ToolBar 工具栏

image.png

image.png

package com.kowaii.mytoolbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.util.Log;
import android.view.View;


public class MainActivity extends AppCompatActivity {

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

        Toolbar toolbar = findViewById(R.id.tbar_one);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("hako","navigationbar back click");
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tbar_one"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#ff0000"
        app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
        app:title="主标题"
        app:subtitle="副标题"
        app:subtitleTextColor="#666666"
        app:titleTextColor="#ffffff"
        app:logo="@drawable/ic_launcher_background"
        app:titleMarginStart="90dp"/>

</LinearLayout>

image.png

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tbar_one"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#ff0000"
        app:navigationIcon="@drawable/ic_baseline_arrow_back_24">


        <TextView android:layout_height="wrap_content" 
            android:layout_width="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="主标题"
            android:textSize="21sp"
            android:textColor="@color/white"
            android:textStyle="normal"
            ></TextView>
        
        
    </androidx.appcompat.widget.Toolbar>

</LinearLayout>

AlertDialog 警告对话框

image.png

自定义View

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#00ffff"
    tools:ignore="MissingDefaultResource">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp" android:src="@drawable/ic_baseline_block_24">

    </ImageView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="这里是自定义View"></TextView>

</LinearLayout>

main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <Button
        android:id="@+id/btn_show"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:text="弹出对话框"
        ></Button>

</LinearLayout>

对话框弹出逻辑代码

package com.kowaii.myalertdialog;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private AlertDialog dialog;

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

        Button btn = findViewById(R.id.btn_show);

        View dialogCustomView = getLayoutInflater().inflate(R.layout.dialog_customview,null);

        dialog = new AlertDialog.Builder(this)
                .setView(dialogCustomView)
                .setIcon(R.drawable.ic_baseline_block_24)
                .setTitle("这是警告对话框")
                .setMessage("这是内容")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.e("hako","点击了取消");
                        dialog.cancel();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.e("hako","点击了确认");
                        dialog.cancel();
                    }
                })
                .create();

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.show();
            }
        });
    }


}

UI效果

image.png

PopupWindow 弹出框

image.png

XML部分

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <Button
        android:id="@+id/btn_show"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:text="弹出窗"
        ></Button>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#00ffff"
    tools:ignore="MissingDefaultResource">

    <Button
        android:id="@+id/custompop_btn1"
        android:layout_width="wrap_content"
        android:layout_height="50dp" android:text="按钮1"></Button>

    <Button
        android:id="@+id/custompop_btn2"
        android:layout_width="wrap_content"
        android:layout_height="50dp" android:text="按钮2" android:layout_marginStart="50dp"></Button>

</LinearLayout>

实现部分

package com.kowaii.mypopupwindow;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

public class MainActivity extends AppCompatActivity {

    private  PopupWindow popupWindow;
    public static final String logTag = "hako";

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

        Button btn = findViewById(R.id.btn_show);

        View contentView = getLayoutInflater().inflate(R.layout.custompopupwindow,null);
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,true);

        Button btn1 = popupWindow.getContentView().findViewById(R.id.custompop_btn1);
        Button btn2 = popupWindow.getContentView().findViewById(R.id.custompop_btn2);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(logTag,"点击了btn1");

                popupWindow.dismiss();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(logTag,"点击了btn2");

                popupWindow.dismiss();
            }
        });


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(popupWindow.isShowing() == true){
                    popupWindow.dismiss();
                }else {
                    popupWindow.showAsDropDown(v);
                }
            }
        });
    }
}

实现效果

image.png