Android初级-Android 日志输出和Debug跟踪,常用控件(中),资源

01-ImageView图片展示组件的使用和属性设置(src属性和background属性的区别)

<ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@drawable/cc"
        />
    <!-- android:src="@drawable/cc" 
        src是显示内容的,图片不会进行拉伸
     android:background="@drawable/cc" 
     background是背景,图片会充满整个控件,会拉伸-->

02-android:layout_gravity与android:gravity的区别

android:gravity 指的是组件内部内容的排列方式  或容器内部組件的排列方式
android:layout_gravity 指的是该组件在父容器中的排列方式

03-ScrollView 和HorizontalScrollView(常用控件讲解)

问题:

1.在水平或者垂直方向组件过多时,会超出手机屏幕可显示的内容

解决

2.使用水平或者垂直的滚动组件通过滑动来显示超出手机屏幕可显示的内容

ScrollView 和HorizontalScrollView    

04-Toast 提示组件的属性和使用


/*
* Toast使用
*   调用静态方法makeText(当前Activity.this,吐司的内容,时长).show():显示
*/
Toast.makeText(ButtonOnClickActivity.this, 
        "通过让自定义类实现OnClickListner接口的方式来实现", 
                                    Toast.LENGTH_LONG).show();

05-View组件以及子类组件设置点击监听,点击事件四种实现方式(Button)

//1.通过让自定义类实现OnClickListner接口的方式来实现
btn_one.setOnClickListener(new MyOnClickListner());
//自定义类
class MyOnClickListner implements OnClickListener{
    @Override
    public void onClick(View v) {
        System.out.println("通过让自定义类实现OnClickListner接口的方式来实现");
        /*
        * Toast使用
        *   调用静态方法makeText(当前Activity.this,吐司的内容,时长).show():显示
        */
        Toast.makeText(ButtonOnClickActivity.this, "通过让自定义类实现OnClickListner接口的方式来实现", Toast.LENGTH_LONG).show();
    }
}
//2.通过匿名内部类来实现点击事件
btn_two.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        System.out.println("通过匿名内部类来实现点击事件");
    }
});


//3.通过让Activity实现OnClickListner接口的方式来实现
btn_three.setOnClickListener(this);
//Activity实现接口
public class ButtonOnClickActivity extends Activity 
                                implements OnClickListener
//重写onClick方法
@Override
public void onClick(View v) {
    System.out.println("通过让Activity实现OnClickListner接口的方式来实现");
} 


//4.通过xml的方式来实现
<Button android:id="@+id/btn_four"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="通过xml方式实现点击事件"
        android:onClick="hh" />
    <!-- android:onClick="hh"   hh就是点击Button按钮时调用的方法的名字 -->

06-Android 日志输出和Debug跟踪

//1.所有信息
Log.v("--Main--", "------------所有信息日志-------------");
//2.debug调试信息
Log.d("--Main--", "------------调试信息日志-------------");
//3.information信息
Log.i("--Main--", "------------信息日志-------------");
//4.warning 警告
Log.w("--Main--", "------------ 警告信息日志-------------");
//5.error错误
Log.e("--Main--", "------------错误信息日志-------------");

07-自定义selector选择器(椭圆(圆) 线 矩形 (圆角) 圆环)press (checked)


//椭圆(圆)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    //渐变色
    <gradient android:startColor="#F80101"
        android:centerColor="#0B20BA"
        android:endColor="#1AAE0E"/>
</shape>

//矩形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    //圆角
    <corners android:radius="30dp"/>
    //填充颜色
    <solid android:color="#FF0000"/>
</shape>

//环形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="ring"
    android:innerRadius="20dp"
    android:thickness="15dp"
    android:useLevel="false">
    <!--  android:innerRadius="20dp" 内部半径
    android:thickness="15dp"    外部半径
    android:useLevel="false" -->
    <!-- <stroke android:color="#0A7241"
        android:width="10dp"/> -->
    <solid android:color="#0A7241"/>
</shape>

//选择器
/*
    使用步骤:
        1.在res下创建drawable目录
        2.在drawable目录下创建一个根标签为selector的xml
        3.在该selector标签下创建两个item,一个item使用一张图片
        4.在按钮上通过background属性来引入使用该selector选择器
*/
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- android:state_pressed="false"组件没有被按下时显示的背景
    android:drawable="@drawable/rectangle_bg"
     -->
    <item android:state_pressed="false" 
        android:drawable="@drawable/rectangle_bg"></item>
     <!-- android:state_pressed="true"组件被按下时显示的背景
    android:drawable="@drawable/rectangle_bg2"
     -->
    <item android:state_pressed="true" 
        android:drawable="@drawable/rectangle_bg2"></item>
</selector>

08-单选按钮(RadioButton)、(RadioGroup)的功能和用法,事件监听(变化监听,按钮点击获取选中内容)

//xml中使用
//RadioGroup代表组,RadioButton代表组的选项,将RadioButton放在RadioGroup内部
<RadioGroup
        android:id="@+id/rg_sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性别:" />

        <RadioButton
            android:id="@+id/rb_nan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男性" />

        <RadioButton
            android:id="@+id/rb_nv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="女性" />
    </RadioGroup>

//为组设置一个被选中的项发生改变的监听器
rg_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
    //被选中项的 id 是 checkedId
    switch (checkedId) {
        case R.id.rb_nan:
        Toast.makeText(RadioGroupActivity.this, "男被选中了",                               Toast.LENGTH_LONG).show();
            break;
        case R.id.rb_nv:
            Toast.makeText(RadioGroupActivity.this, "女被选中了", 
                         Toast.LENGTH_LONG).show();
        break;
        default:
        break;
        }
    }
});

//判断选中的是性别
int crbId = rg_sex.getCheckedRadioButtonId();//获取备选中的单选按钮Id
if(crbId == R.id.rb_nan){
    sb.append("性别:男");
}else {
    sb.append("性别:女");
}

09-复选框(CheckBox)的功能和用法;事件监听(变化监听,按钮点击获取选中内容)

<!-- 定义喜欢的颜色 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="喜欢的颜色:" />
        <CheckBox android:id="@+id/cb_red"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="红色" />
        <CheckBox android:id="@+id/cb_blue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="蓝色" />
        <CheckBox android:id="@+id/cb_green"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绿色" />
    </LinearLayout>

//为复选框设置被选中事件
cb_red.setOnCheckedChangeListener(new 
                        CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked){
        Toast.makeText(RadioGroupActivity.this, "红色被选中了",                                     Toast.LENGTH_LONG).show();
    }else {
        Toast.makeText(RadioGroupActivity.this, "红色未被选中了",                                   Toast.LENGTH_LONG).show();
    }
}
});

cb_blue.setOnCheckedChangeListener(new 
        CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked){
        Toast.makeText(RadioGroupActivity.this, "蓝色被选中了",                                  Toast.LENGTH_LONG).show();
    }else {
        Toast.makeText(RadioGroupActivity.this, "蓝色未被选中了",                                  Toast.LENGTH_LONG).show();
    }
}
});

//获取复选框中的内容
StringBuffer sb = new StringBuffer();
sb.append("\n喜欢的颜色:");
//判断选中的颜色
if(cb_red.isChecked()){
sb.append("红色");
}

if(cb_blue.isChecked()){
sb.append("蓝色");
}
if(cb_green.isChecked()){
sb.append("绿色");
}
Toast.makeText(RadioGroupActivity.this, sb.toString(), 
                                Toast.LENGTH_LONG).show();

image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容