说明书大全网 - 海量说明书在线查询
手机版  |  网站地图

switchermod怎么用(ImageView、Switch以及DatePicker的使用)

一、各自的功能及使用场景

ImageView:用于展示一张图片,例如商品展示;

Switch:开关、控制器,常用于app设置中;

DatePicker:日期选择器,多用于订阅类的服务和功能

二、简单使用

1、布局部分

<?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"
    tools:context=".MainActivity"
    android:gravity="center"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/iv_img"
        android:layout_width="100dp"
        android:layout_height="100dp" />
    <Button
        android:id="@+id/btn_todo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CLICK"/>
    <Switch
        android:id="@+id/s_toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"/>
    <TextView
        android:id="@+id/tv_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <DatePicker
        android:id="@+id/dp_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

2、代码部分

  private TextView tvDate;
    private Button btnTodo;
    private Switch sToggle;
    private DatePicker dpDate;
    private ImageView ivImg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListener();
        ivImg.setImageResource(R.drawable.ic_launcher_foreground);
    }

    private void setListener() {
       btnTodo.setOnClickListener(v -> {
           Toast.makeText(this, "按钮被点击", Toast.LENGTH_SHORT).show();
       });
       sToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               btnTodo.setEnabled(isChecked);
           }
       });

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            dpDate.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    tvDate.setText(year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日");
                }
            });
        }
    }

    private void initView() {
        tvDate=findViewById(R.id.tv_date);
        btnTodo=findViewById(R.id.btn_todo);
        sToggle=findViewById(R.id.s_toggle);
        dpDate=findViewById(R.id.dp_date);
        ivImg=findViewById(R.id.iv_img);
    }
}

代码中,我通过sToggle来控制btnTodo是否可点击,将dpDate获取到的日期用tvDate展示,之所以会+1是因为月份获取到的数字是从0开始的

三、运行效果


四、官网对于上述控件的属性介绍

1、ImageView

android:adjustViewBounds

Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

android:baseline

The offset of the baseline within this view.

android:baselineAlignBottom

If true, the image view will be baseline aligned with based on its bottom edge.

android:cropToPadding

If true, the image will be cropped to fit within its padding.

android:maxHeight

An optional argument to supply a maximum height for this view.

android:maxWidth

An optional argument to supply a maximum width for this view.

android:scaleType

Controls how the image should be resized or moved to match the size of this ImageView.

android:src

Sets a drawable as the content of this ImageView.

android:tint

The tinting color for the image.

android:tintMode

Blending mode used to apply the image tint.

2、Switch

android:showText

Whether to draw on/off text.

android:splitTrack

Whether to split the track and leave a gap for the thumb drawable.

android:switchMinWidth

Minimum width for the switch component.

android:switchPadding

Minimum space between the switch and caption text.

android:switchTextAppearance

TextAppearance style for text displayed on the switch thumb.

android:textOff

Text to use when the switch is in the unchecked/"off" state.

android:textOn

Text to use when the switch is in the checked/"on" state.

android:textStyle

Style (normal, bold, italic, bold|italic) for the text.

android:thumb

Drawable to use as the "thumb" that switches back and forth.

android:thumbTextPadding

Amount of padding on either side of text within the switch thumb.

android:thumbTint

Tint to apply to the thumb.

android:thumbTintMode

Blending mode used to apply the thumb tint.

android:track

Drawable to use as the "track" that the switch thumb slides within.

android:trackTint

Tint to apply to the track.

android:trackTintMode

Blending mode used to apply the track tint.

android:typeface

Typeface (normal, sans, serif, monospace) for the text.

3、DatePicker


android:calendarTextColor

The text color list of the calendar.

android:calendarViewShown

Whether the calendar view is shown.

android:datePickerMode

Defines the look of the widget.

android:dayOfWeekBackground

The background color for the header's day of week.

android:dayOfWeekTextAppearance

The text color for the header's day of week.

android:endYear

The last year (inclusive), for example "2010".

android:firstDayOfWeek

The first day of week according to Calendar.

android:headerBackground

The background for the selected date header.

android:headerDayOfMonthTextAppearance

The text appearance for the day of month (ex.

android:headerMonthTextAppearance

The text appearance for the month (ex.

android:headerYearTextAppearance

The text appearance for the year (ex.

android:maxDate

The maximal date shown by this calendar view in mm/dd/yyyy format.

android:minDate

The minimal date shown by this calendar view in mm/dd/yyyy format.

android:spinnersShown

Whether the spinners are shown.

android:startYear

The first year (inclusive), for example "1940".

android:yearListItemTextAppearance

The list year's text appearance in the list.

android:yearListSelectorColor

The list year's selected circle color in the list.

五、补充

一般来讲,有什么属性,就有对应的set和get方法;另外对代码中的if语句块作个简单说明:它表示android版本大于等于8.0以上才执行if里的内容