Android 开发

Android

第一章 布局

activity_main 详解
<LinearLayout>
    
</LinearLayout>

View

 View.GONE 控件属性 是否隐藏
 View.VISIBLE 控件属性 显示

Log

logd debug
    logi info
    logw warn
    logt TAG

data标签

<data></data>
scheme  用于指定数据协议
host       指定数据的主机名部分
port       指定数据的端口部分
path       指定主机名和端口之后的部分
mimeType   指定可以处理的数据类型,允许通配符

java中的活动终止:finish();

Android的基本控件一:文本控件(TextView)
控件属性
1、layout_width 组件宽度
     * 取值:
        1、match_parent : 父容器的宽度
        2、wrap_content : 由控件内容决定宽度(不能超过容器)
        3、给出明确值   25dp 
2、layout_height 组件高度
   Android:layout_margin
   android: layout_padding   
                       
3、id:  组件的id
        语法:android:id="@+id/str_title"
        用于Java程序获取控件
4、text 设置显示的文本内容
5、textColor:设置字体颜色
              Android:textColor="#ff000000"
               ff为是否透明
               00 为三原色 红绿蓝
6、textSttyle : 设置字体风格,nonrmal(无效果)   bold(加粗)   italic(斜体)
7、textSize:字体大小,单位一般是用sp
8、background控件的背景颜色     可以是图片
9、gravity   设置控件内容的对齐方式, textview中是文字,imageview中是图片
                   设置控件位置 center 

java获取Android 控件

findById(R.id.属性id);

带阴影的textView

1 android :shadowColoe:设置阴影颜色,需要与showRadius一起使用

2 android:shadowRadius:设置阴影模糊程度,设为0.1就变成字体颜色,建议使用3.0

3 Android:shadowDx设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标

3 Android:shadowDy设置阴影在垂直方向的偏移,就是垂直方向阴影开始的纵坐标

  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/t1"
        android:text="@string/app_name"
        android:shadowColor="@color/black"
        android:shadowRadius="3.0"
        android:shadowDy="10.0"
        android:shadowDx="10.0"
        ></TextView>

实现跑马灯效果

1,Android:singleLine:内容单行显示 true

2、Android:focusable 是否获取焦点

3、android:fousablelnTouchMode:用于控制视图在触摸模式下是否可以聚集

4、android:ellipsize:在哪里省略文本

5、android : marqueeRepeatLimit:字幕动画重复的次数

第一种获取焦点的方法
<!--        android:clickable="true"-->


第二种处理方式
<org.Dwx.androiddemo.MyTextView
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:id="@+id/t1"
        android:text="@string/app_name"
        android:shadowColor="@color/black"
        android:shadowRadius="3.0"
        android:shadowDy="10.0"
        android:shadowDx="10.0"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusableInTouchMode="true"
        android:focusable="true"

        ></org.Dwx.androiddemo.MyTextView>

java
package org.Dwx.androiddemo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.annotation.Nullable;


@SuppressLint("AppCompatCustomView")
public class MyTextView  extends TextView {
    public MyTextView(Context context) {
        super(context);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

第三种:
  <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/t1"
        android:text="@string/hbh"
        android:shadowColor="@color/black"
        android:shadowRadius="3.0"
        android:shadowDy="10.0"
        android:shadowDx="10.0"
        android:textSize="25sp"
        android:textColor="@color/design_default_color_primary_dark"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusableInTouchMode="true"
        android:focusable="true"
        >
        <requestFocus/>
    </TextView>
Android 的基本控件:button

StateListDrawable是Drawable资源的一种,可以根据不同的状态,设置不同的图片效果,关键结点 ,我们只需要将button 的background 属性设置为改drawable 资源即可轻松实现,按下按钮时不同按钮颜色或背景

1、drawable:引用的Drawable位图

2、state_focused:是否获取得焦点

3、state_pressed:控件是否被按下

4、state_enabled:控件是否可用

5、state_selected:控件是否被选择,针对滚轮得情况

6、state_checked:控件是否被勾选

7、state_checkable:控件可否被勾选,eg:checkbox

8、state_windows_focused:是否获取窗口焦点

9、state_active:控件是否处于活动状态,eg:slidingTab

10、state_single:控件包含多个子控件时,确定是否只显示一个子控件

11、state_first:控件包含多个子控件时 ,确定第一个子控件是否处于显示状态

12、state_middle:控件包含多个字控件时,确定中间一个子控件是否处于显示状态

13、state_last:控件包含多个子控件时,确定最后一个子控件是否处于显示状态

showToast:提示框 <在active 中设置onCick>

在java里面写

public void showToast(){
    Toast.makeText(this,"我被点击了",Toast.LENGTH_SHORT).show()
}
   button 的自定义样式
   <drawabler > 
   <shape>
      <solid 
             //填充式
             android:color="#ff9900"
            />
         <corners
                  android: radius="5dp"
                  />
   </shape>
   //描边        
       android:width="1dp"
       android:color="#ff9900"
       <corners
               android: radius="5dp" 
                />
    </stroke>
   backgroundTint="@color/btn_color_selector" //如果设置了按钮图片 它可以设置图片颜色
   
   android : forground="#000000"//填充按钮   前景色
   textAllCaps  开启小写转换大写   默认为true   改falsebutton将不再改变大小写
   
   java的添加监听事件
    button事件
    myBtn = findViewById(R.id.btn1);
            myBtn.setOnClickListener(new View.OnClickListener() { //点击事件
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this,TestActivity2.class);
    // Intent(映射当前类,目的类);
                    startActivity(intent);
                }
            });
   //长按事件
   
   // 触摸事件
   /点击
           btn1.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   
               }
           });
           //长按事件
           btn1.setOnLongClickListener(new View.OnLongClickListener() {
               @Override
               public boolean onLongClick(View v) {
                   return false;
               }
           });
           // 触摸事件
           btn1.setOnTouchListener(new View.OnTouchListener() {
               @Override
               public boolean onTouch(View v, MotionEvent event) {
                   return false;
               }
           });
   
       /*
           * 当onTouch的返回值为true时 由于条件判断的原因
           * onLongClick会不执行
           * Click也不会被执行
           *
           * 当当onTouch的返回值为False时 longClick返回值为true时,只会阻塞click
           * */
Intent
Intent 分为两种显式Intent 和 隐式Intent
 显式Intent
 Intent intent = new Intent(MainActivity.this,TestActivity2.class);
 // Intent(映射当前类,目的类);
startActivity(intent);
隐式
  <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="org.Dwx.activitytest.ACTION_START"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>
        </activity>

EditText输入框

文本提示:
特有属性:
hit输入提示
textColorHint输入提示的文字的颜色
inputType输入类型
DrawableXxxx在输入框的指定方位添加图片
DrawablePaddingXxxx 设置内容与边距的间距
background 背景色

Android:hint="用户名"
输入数据隐藏
inputType="textpassword"

<EditText
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:textSize="24sp"
        android:textColor="#000000"
        android:hint="用户名"
        android:background="@drawable/et_username"
        android:id="@+id/et1"
        android:maxLines="1"
        android:ellipsize="start"
        android:maxLength="16"
        android:paddingLeft="10dp"
        >
    
    
    java获取输入文本
    getTetx().tostring()

RadioButton:

   <RadioGroup
       android:id="@+id/rg1"
       android:layout_width="100dp"
       android:layout_height="100dp"
       android:orientation="vertical"
       >
       <RadioButton
           android:layout_width="50dp"
           android:layout_height="50dp"
           android:text=""
           />
       <RadioButton
           android:layout_width="50dp"
           android:layout_height="50dp"
           android:text=""
           >

       </RadioButton>
   </RadioGroup>
自定义样式:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <shape>
            <stroke android:color="#f09394"></stroke>
            <corners android:radius="40dp"></corners>
        </shape>
    </item>
    <item android:state_checked="false">
        <shape>
            <solid android:color="#f09394"
                ></solid>
        </shape>
    </item>
</selector>

java设置监听事件
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rb = (RadioGroup) findViewById(R.id.rg1);
        rb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
               RadioButton rab = findViewById(checkedId);
                Toast.makeText(MainActivity.this,rab.getText(),Toast.LENGTH_SHORT).show();
            }
        });

复选框:

  <LinearLayout
        android:id="@+id/l"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <CheckBox
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="PC"
            >
        </CheckBox>
        <CheckBox
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="andoid"
            >
        </CheckBox>
        <CheckBox
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Mac"
            >
        </CheckBox> <CheckBox
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="IOS"
        >
    </CheckBox>
        <CheckBox
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Unix"
        >
    </CheckBox>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="我是分割线"
        >

    </TextView>
    </LinearLayout>
      <CheckBox
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/cf"
          android:text="吃饭"
          android:layout_below="@+id/l"
          >

      </CheckBox>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/sj"
        android:layout_below="@+id/cf"
        android:text="睡觉"
        >

    </CheckBox>


  cf = findViewById(R.id.cf);
        sj = findViewById(R.id.sj);
        cf.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(MainActivity.this,isChecked?"吃饭啦!吃饭啦!" : "饿着吧QAQ",Toast.LENGTH_SHORT).show();
            }
        });
        sj.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(MainActivity.this,isChecked?"睡觉啦!睡觉啦!" : "我不困QAQ",Toast.LENGTH_SHORT).show();
            }
        });

Image

button 的衍生控件 ToggleButton , switch 
主要属性:
src
scaleType
maxHeight最大高度
maxWidth
adjustViewBounds 调整界限 使图片不超过最大值
<ImageView
     android:layout_width="200dp"
     android:layout_height="200dp"
     android:id="@+id/logo"
     android:src="@drawable/home"
     android:scaleType="fitXY"  //铺满整个容器
     >
 </ImageView>

scaleType属性:
fisStart:保持宽高比说放图片,直到较长的边与image的边长相等
fitEnd缩放放于右下角
fitXY:撑满控件,宽高比可能发生改变
fitCenter:保持宽高比缩放,直至能够完全显示
center 保持原图大小,显示在imge中心
centerCrop:保持宽高比缩放,直至完全覆盖控件,裁剪显示
centerInside 保持宽高比,知道iamge能够完全显示图片   如果图片尺寸大于image则等比缩放 
matrix 不改变原图大小,从image的左上角开始绘制原图,超出部分裁剪

加载网络图片
1、获取网络权限:uses-permission android:name = "android.permission.INTERNET"
使用bumptech glide 对象
使用 Glide.with(this).load().into(Image)

 <ImageView
      android:layout_width="300dp"
      android:layout_height="300dp"
      android:scaleType="fitXY"
      android:background="#dddddd"
      android:id="@+id/img1"
      >

  </ImageView>
   ImageView img = findViewById(R.id.img1);
        Glide.with(this).load("http://blog.xirang.ltd/img/Java.png").into(img);
Menu 控件
/*
右上角的小菜单栏
*/
需要在layout下创建menu文件夹,再创建具体的menu文件
    
    
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/m_1"
    android:title="添加"
    >
</item>
    <item
        android:id="@+id/m_2"
        android:title="删除"
        ></item>
</menu>MainActivity.java
            重写以下方法
             @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return  true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.m_1:
                Toast.makeText(this,"添加",Toast.LENGTH_SHORT).show();
                break;

            case R.id.m_2:
                Toast.makeText(this,"删除",Toast.LENGTH_SHORT).show();
                 break;
            default:
                break;
        }
        return true;
    }
ProgressBar:
加载圈
max: 进度条的最大值
progress:进度条完成值
indeterminate:如果设置true,则进度条不精确显示进度
style="android:attr/propressBarStyleHorizontal" 水平进度条 

<ProgressBar
    android:id="@+id/pr1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ></ProgressBar>
<Button
    android:id="@+id/btn1"
    android:layout_below="@id/pr1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="隐藏"
    android:onClick="hiddenProgress"
    />

    <ProgressBar
        android:layout_below="@id/btn1"
        android:id="@+id/pr2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:max="100"
        style="?android:attr/progressBarStyleHorizontal"
        ></ProgressBar>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn2"
            android:text="点我加进度"
            android:layout_below="@id/pr2"
            ></Button>

java
 pr1 = findViewById(R.id.pr1);
        pr2 = findViewById(R.id.pr2);
        btn = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int progress = pr2.getProgress();
                progress+=10;
                pr2.setProgress(progress);
            }
        });
    }
    public void hiddenProgress(View view) {
        if(pr1.getVisibility() == view.GONE){
            pr1.setVisibility(View.VISIBLE);
            btn.setText("隐藏");
        }else{
            pr1.setVisibility(view.GONE);
            btn.setText("显示");
        }
    }
NotificationManager

NotificationManager类是一个通知管理器类,这个对象是由系统维护的服务,是以单例模式的方式获取,所以一般并不是直接实例化这个对象。在Activity中,使用Acivity.getSystemService(方法获取)

NotificationManager对象,Acivity.getSystemService方法可以通过android系统服务的句柄,返回对应得对象。在这里需要返回

NotificationManager,所以直接传递Context.NOTIFICATION_SERVICE即可

使用builder构造器来创建Notification对象

使用NotificationCompat类得builder构造器来创建Notification对象,可以保证程序在所有得版本上都能正常工作。Android8.0 新增了通知渠道这个概念,如果没有设置,则无法在android8.0机器上显示

NotificationChannel

通知渠道:

IMPORTANCE_NONE 关闭通知

IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示

IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示

IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示

IMPORTANCE_HIGH 开启通知,会弹出,会发出提示音,状态栏中显示

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn1"
    android:text="发出通知"
    android:onClick="send"
    ></Button>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn1"
        android:text="取消通知"
        android:onClick="close"
        ></Button>
 manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel("massage","测试",
                    NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }
        Intent intent = new Intent(this, contentIntentActivity.class);
        PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
        notification = new Notification.Builder(this,"massage")
                .setContentTitle("你好啊")
                .setContentText("Hello World")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setColor(Color.parseColor("#ff000000"))
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.android))
                .setContentIntent(activity)
                .setAutoCancel(true)
                .build();
    }

    public void send(View view) {
manager.notify(1,notification);
    }

    public void close(View view) {
        manager.cancel(1);
    }
notification 设置项
    setContentTitle()  设置标题
    setContentText()  设置文本内容
    setSmallIcon()设置小图标
    setLargelIcon()设置通知大的大图标
    setColor()设置小图标得颜色
    setContentIntent设置点击通知后得跳转意图
    setAutoCancel()设置点击通知后自动清除
    setWhen()设置通知被创建得时间

android5.0开始,所有应用程序得通知栏图标,只能使用alpha图层 而不能使用RGB图层

ToolBar
layout_widyh="match_parent"
layout_height="?attr/actionBarSize"
background
navigationIcon="@drawable/ic_....."
app:title="主标题"
app:titleColor=""
app:titleMarginStart=""
app:subtitle=
app:subtitleTextColor
app:logo="@mipmap/ic_...."


<androidx.appcompat.widget.Toolbar
    android:id="@+id/tb"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="#ffff00"
    app:logo="@drawable/ic_launcher_foreground"
    app:navigationIcon="@drawable/android"
    app:title="哈撒给"
    app:subtitle="子标题"
    app:subtitleTextColor="#ff0000ff"
    app:titleMarginStart="20dp"
    app:titleTextColor="#ff0000"
    >
</androidx.appcompat.widget.Toolbar>

    <androidx.appcompat.widget.Toolbar
        android:layout_below="@id/tb"
        android:id="@+id/tb1"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#ffff00"
        app:subtitleTextColor="#ff0000ff"
        app:titleMarginStart="20dp"
        app:titleTextColor="#ff0000"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hello"
            android:layout_gravity="center"
            >
        </TextView>
    </androidx.appcompat.widget.Toolbar>
AlertDialog
实现方式: 
AlertDialog.Builder builder = new AlertDialog.Builder(Context); 构建各种参数 
Builder.setIcon();添加ICON
Builder.setTile();添加标题
Builder.setMassage();添加消息
Builder.setView();设置自定义布局
Builder.create();创建Dialog
Builder.show();显示对话框
setPositiveButton 确认按钮
setNegativeButton 取消按钮
setNeutralButton  中间按钮
main
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="显示弹窗"
    android:onClick="showAlert"
    />
dialog
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/android"
    />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="憨包花的小可爱"
        ></TextView>
java

 public void showAlert(View view) {
        View inflate = getLayoutInflater().inflate(R.layout.dialog, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_launcher_foreground);
        builder.setTitle("你好,我是憨包花");
        builder.setMessage("阿巴阿巴!憨包花");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"嘻嘻嘻!憨包花",Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"哈哈哈哈哈!憨包花",Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNeutralButton("中间", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"打死憨包花!!!!",Toast.LENGTH_SHORT).show();
            }
        });
         builder.setView(inflate);
        builder.create();
        builder.show();
    }
PopupWindow
setContentView() 设置PopupWindow 显示的View
showAsDropDown() 相对某个控件的位置(正下方),无偏移
showAsDropDown()相对某个控件的位置有偏移
setFocusable()设置是否获取焦点
setBackgroundDarwable()设置背景
dismiss()关闭弹窗
setAnimationStyle()设置加载动画
setTouchable()设置触摸使能           点击在PopupWindow范围内            //影响点击事件
setOutsodeTouchable 设置PopupWindow 外面的触摸使能点击在PopupWindow范围外 //影响点击事件

  <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/ic_launcher_background"
        >
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn1"
    android:text="北京"
    ></Button>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上海"
        android:id="@+id/btn2"
        ></Button>
    </LinearLayout>


 public void showAlert(View view) {
        View inflate = getLayoutInflater().inflate(R.layout.activity_popup,null);
         btn1 = inflate.findViewById(R.id.btn1);
        btn2 = inflate.findViewById(R.id.btn2);
        popupWindow = new PopupWindow(inflate,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                true
                );
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.android));
          popupWindow.showAsDropDown(view);//弹出位置
       // popupWindow.showAsDropDown(view,view.getWidth(),-view.getHeight());
/*
* inflate,//View
                ViewGroup.LayoutParams.WRAP_CONTENT, 高  可以写实际数值
                ViewGroup.LayoutParams.WRAP_CONTENT   宽
                );
*
* */
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("北京");
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("上海");
                popupWindow.dismiss();
            }
        });
布局
LinearLayout
orientation  布局中组件的排列方式
                        vertical 水平布局
                        horizontal  垂直布局
gravity   控制组件所包含的子元素的对齐方式
layout_gravity  控制该组件在父容器里的对齐方式
background  为该组件设置一个背景图片,或者是直接用颜色覆盖
divider     分割线
showDividers 设置分割线所在位置  none(无)  beginning (开始) end(结束) middle(每两个组件间)
dividerPadding 设置分割线的padding
layout_weight 权重   该属性是用来等比例划分区域

使元素平分一个容器
android:layout_width="0dp"
 android:layout_weight="1" 权重

                   

RelativeLayout
相对布局 :RelativeLayout
<RelativeLayout></RelativeLayout>

layout_alignParentLeft 左对齐
layout_alignParentRight 左对齐
layout_alignParentTop 顶部对齐
layout_alignParentBottom 底部对齐
layout_centerVertical 垂直对齐
layout_centerParent 垂直对齐

layout_toLeftOf     放置于参考组件的左边
layout_toRightOf    放置于参考组件的右边             
layout_above        放置于参考组件的上方
layout_beliow       放置于参考组件的下方
layout_alignTop     放置于参考组件的上边界
layout_alignBottom  放置于参考组件的下边界
layout_alignLeft    放置于参考组件的左边界
layout_alignRight   放置于参考组件的右边界  


通用属性
margin    and     padding
FrameLayout
主要属性:
foreground:前景色(背景)
foregroundGravity:前景位置

主要特性:后加的覆盖之前的
TableLayout
android:collapseColumns  设置需要被隐藏的列的序号,从0开始 (0,2)
android:stretchColumns 设置允许被拉伸的列的列序号,从0开始 (1) 结合collaspseColumns 
android:shrinkColumns 设置允许被收缩的列的列序号,从0开始  (1)

子控件设置属性
android:layout_column 显示在第几列
android:layout_span  横向跨几列


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="1"
    android:collapseColumns=""
    android:stretchColumns="2"
    >
    <TableRow
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="显示弹窗"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="显示弹窗"
            />
        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示弹窗"
        />
        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示弹窗"
        />
        <Button
            android:layout_column="1"
            android:layout_span="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示弹窗"
        />
    </TableRow>

</TableLayout>
GridLayout
网格布局
主要属性:
        orientation   设置水平显示还是垂直显示
                        vertical 水平布局
                        horizontal  垂直布局
        columnCount   设置行的显示个数
        rowCount      设置列的显示个数
子控件属性:
layout_column        显示在第几列
layout_columnSpan    横向跨几列
layout_columnWeight  横向剩余空间分配方式
layout_gravity       在网格中的显示位置
layout_row           显示在第几行
layout_rowSpan       横向跨几行
layout_roweight      纵向剩余空间分配方式
ConstraintLayout
可视化页面布局(鼠标拖动)

第二章

Android基础开发
listView
常用属性:
主窗体
 <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/list"
        >
    </ListView>
list控件
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text1"
        ></TextView>
java代码实现
package org.Dwx.androiddemo.listView;

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

import org.Dwx.Bean.User;
import org.Dwx.androiddemo.R;

import java.util.List;

public class MyAdapter extends BaseAdapter {

    private List<User> list;
    private Context context;

    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       if(convertView == null){
           convertView = LayoutInflater.from(context).inflate(R.layout.activity_list, parent,false);
       }
        TextView text = convertView.findViewById(R.id.text1);
       text.setText(list.get(position).getName());
        System.out.println(position);
        return convertView;
    }

    public MyAdapter(List<User> list, Context context) {
        this.list = list;
        this.context = context;
    }
}

        
        
 List<User> list = new ArrayList<>() ;
       for (int i=0;i<=100;i++){
       User u = new User();
       u.setName("憨包花"+i+"号");
       list.add(u);
       }
        ListView listView = findViewById(R.id.list);
        listView.setAdapter(new MyAdapter(list,this));       
RecyclerView
自定义Activity

Activity:相当于一个类

//新版本特性 不继承与Activity
public class listActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list2);
    }
}

错误处理

//你的主机中的软件中止了一个已建立的连接。
   解决方式:关闭WiFi
//设置背景样式无效
       设置values下的themes中
       <style name="Theme.Android" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
       设置为
        <style name="Theme.Android" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">

代码整理1


<!--     <LinearLayout-->
<!--         android:layout_width="200dp"-->
<!--         android:layout_height="200dp"-->
<!--         android:background="#000000"-->
<!--         android:id="@+id/div1"-->
<!--         android:orientation="vertical">-->
<!--         <LinearLayout-->
<!--             android:layout_width="match_parent"-->
<!--             android:layout_height="match_parent"-->
<!--             android:background="#ff0000"-->
<!--             android:layout_margin="20dp"-->
<!--             >-->
<!--         </LinearLayout>-->
<!--     </LinearLayout>-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="horizontal"
        android:id="@+id/d2"
        android:background="#000000"
       >
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="#ffffff"
                android:layout_weight="1"
                >
            </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:background="#000000"
                    android:layout_weight="1"
                    >
            </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#ff0000"
            android:layout_weight="1"
            >
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@color/material_on_surface_stroke"
            android:layout_weight="1"
            >
        </LinearLayout>
</LinearLayout>
<!--    <TextView-->
<!--        android:layout_width="wrap_content"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:id="@+id/str_title"-->
<!--        android:textSize="25sp"-->
<!--        android:text="@string/str_title"-->
<!--         android:singleLine="true"-->
<!--        android:focusable="true"-->
<!--        android:focusableInTouchMode="true"-->
<!--        android:ellipsize="end"-->
<!--        android:marqueeRepeatLimit="10"-->
<!--        android:clickable="true"-->
<!--        app:layout_constraintBottom_toBottomOf="parent"-->
<!--        app:layout_constraintHorizontal_bias="0.011"-->
<!--        app:layout_constraintLeft_toLeftOf="parent"-->
<!--        app:layout_constraintRight_toRightOf="parent"-->
<!--        app:layout_constraintTop_toTopOf="parent"-->
<!--        app:layout_constraintVertical_bias="0.022"-->

<!--        />-->
<!--    <Button-->
<!--        android:id="@+id/button2"-->
<!--        android:layout_width="wrap_content"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:text="点我变身"-->
<!--        tools:layout_editor_absoluteX="127dp"-->
<!--        tools:layout_editor_absoluteY="160dp" />-->

代码整理2

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/btn_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/v1"
        android:background="#000000"
        >
    </View>
    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#ff0000"
        android:layout_below="@+id/v1"
        >
    </View>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/l1"
        android:orientation="horizontal"
        android:background="#ff00ff"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:padding="15dp"
        >
         <View
             android:layout_width="100dp"
             android:layout_height="match_parent"
             android:id="@+id/lv1"
             android:background="#ff0000"
             >
         </View>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000000"
            android:padding="15dp"
            >
            <View
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:background="#f0f0ff"
                 android:id="@+id/v3"
                >
            </View>
            <View
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:background="#f0f0ff"
                android:layout_toRightOf="@+id/v3"
                android:layout_marginLeft="5dp"
                >

            </View>
       </RelativeLayout>

    </LinearLayout>
</RelativeLayout>

代码整理3:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/btn_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:text="textView"
        android:textSize="25dp"
        android:id="@+id/btn1"
        android:textColor="@color/black"
        >
    </Button>
    <Button
       android:layout_below="@+id/btn1"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:text="textView"
        android:id="@+id/btn2"
        android:background="@drawable/btn_1"
        >

    </Button>
    <Button

        android:layout_below="@id/btn2"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:text="textView"
        android:id="@+id/btn3"
        android:background="@drawable/btn_4"
        >

    </Button>

    <Button

        android:layout_below="@id/btn2"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:text="textView"
        android:id="@+id/btn4"
        android:background="#ff4532"
        android:onClick="showMessage"
        >

    </Button>
</RelativeLayout>
drawble

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ff9900"
    >
</solid>
    <corners android:radius="10dp"/>
</shape>


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
       android:state_pressed="true"
        >
        <shape>
            <solid android:color="@color/black"/>
        </shape>
    </item>
    <item android:state_pressed="false">
        <shape>
            <solid android:color="#ff9900"/>
        </shape>
    </item>
</selector>

代码整理四:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/btn_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:gravity="center"
    >
    <EditText
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:textSize="24sp"
        android:textColor="#000000"
        android:hint="用户名"
        android:background="@drawable/et_username"
        android:id="@+id/et1"
        android:maxLines="1"
        android:ellipsize="start"
        android:maxLength="16"
        android:paddingLeft="10dp"
        >

    </EditText>
    <EditText
        android:layout_marginTop="15dp"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:textSize="24sp"
        android:textColor="#000000"
        android:hint="密码"
        android:id="@+id/et2"
        android:layout_below="@id/et1"
        android:inputType="textPassword"
        android:maxLines="1"
        android:ellipsize="start"
        android:background="@drawable/et_username"
        android:paddingLeft="10dp"
        >

    </EditText>
    <Button
        android:layout_marginTop="25dp"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_below="@id/et2"
        android:text="登录"
        android:background="#FF6200EE"
        android:textColor="@color/white"
        android:textSize="25dp"
        android:layout_marginLeft="50dp"
        android:id="@id/login_btn"
        >

    </Button>
</RelativeLayout>


Edit监听事件
  <code>et = findViewById(R.id.et1);
        System.out.println(et.getText());
         et.addTextChangedListener(new TextWatcher() {
             @Override
             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
             //输入前
             }

             @Override
             public void onTextChanged(CharSequence s, int start, int before, int count) {
                 System.out.println("message:"+s.toString());
           正在输入
             }

             @Override
             public void afterTextChanged(Editable s) {
          输入后
             }
         });
      </code>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/btn_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:gravity="center"
    >
   <RadioGroup
       android:id="@+id/rg1"
       android:layout_width="100dp"
       android:layout_height="100dp"
       android:orientation="vertical"
       >
       <RadioButton
           android:layout_width="50dp"
           android:layout_height="50dp"
           android:text=""
           />
       <RadioButton
           android:layout_width="50dp"
           android:layout_height="50dp"
           android:text=""
           >

       </RadioButton>
   </RadioGroup>

    <RadioGroup
        android:layout_below="@id/rg1"
        android:id="@+id/rg2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:orientation="vertical"
        >
        <RadioButton
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:gravity="center"
            android:text=""
            android:background="@drawable/rg_style"
            android:button="@null"

            />
        <RadioButton
            android:layout_width="50dp"
            android:gravity="center"
            android:layout_height="50dp"
            android:text=""
            android:background="@drawable/rg_style"
            android:button="@null"
            >

        </RadioButton>
    </RadioGroup>
</RelativeLayout>


     private Button myBtn;
        private EditText et;
        private RadioGroup rb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rb = (RadioGroup) findViewById(R.id.rg1);
        rb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
               RadioButton rab = group.findViewById(checkedId);
                Toast.makeText(MainActivity.this,rab.getText(),Toast.LENGTH_SHORT).show();
            }
        });
    }

}

代码整理4:

     manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel("massage","测试",
                    NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }
        Intent intent = new Intent(this, contentIntentActivity.class);
        PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
        notification = new Notification.Builder(this,"massage")
                .setContentTitle("你好啊")
                .setContentText("Hello World")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setColor(Color.parseColor("#ff000000"))
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.android))
                .setContentIntent(activity)
                .setAutoCancel(true)
                .build();
    }

    public void send(View view) {
manager.notify(1,notification);
    }

    public void close(View view) {
        manager.cancel(1);
    }
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值