ShapeDrawable,Style,AlertDialog

ShapeDrawable

用来自定义一些样式,如按钮点击按下,CheckBox的选择,进度条的背景和进度等。

Shape 标签
可以用来定义边框,圆角,梯度渐变色。
一般用于Button,或Layout背景。

shape:指定形状
  矩形:rectangle
  椭圆:oval
  横线:line
  圆环:ring

solid:指定shape中填充的颜色

stroke:指定shape的边框
  描边的颜色:android:color
  描边的宽度:android:width
  组成虚线的线段的宽度:android:dashWidth
  组成虚线的线段之间的间隔: android:dashGap

corners:指定矩形四个角的圆角程度
  给四个角设置相同的角度:android:radius
  设定左下角的角度:android:bottomLeftRadius
  设定右下角的角度:android:bottomRightRadius
  设定左上角的角度:android:TopLeftRadius
  设定右上角的角度:android:TopRightRadius

gradient:指定填充颜色的渐变
  渐变类别:android:type 
  linear(线性渐变)、radial(径向渐变)、sweep(扫描线渐变,默认为linear 

  渐变的角度:android:angle
  默认为0,其值必须是45的倍数,0表示从左到右,90表示从下到上
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >

    <solid android:color="#C1C1C1" />

    <stroke
        android:width="8dp"
        android:color="#000"
        android:dashGap="2dp"
        android:dashWidth="3dp"
        />

    <corners android:radius="4dp" />

    <gradient
        android:type="linear"
        android:centerColor="@color/colorPrimary"
        android:endColor="@color/colorPrimaryDark"
        android:startColor="@color/colorAccent" />

</shape>
效果
selector 标签
用来控制选中按钮状态的改变
<?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="#EEEEE0" />
            <corners android:radius="4dp" />
        </shape>
    </item>

    <item>
        <shape>
            <solid android:color="#FFEBCD" />
            <corners android:radius="4dp" />
        </shape>
    </item>
</selector>

控制按钮文本按下后的状态改变
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#EE0000"/>
    <item android:color="#E9967A"/>
</selector>
效果
CheckBox选中状态的改变
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 一定是选中的在前,未选中的在后,用state_checked标记 -->
    <item android:state_checked="true"
        android:drawable="@drawable/more_radio_selected" />
    
    <item android:drawable="@drawable/more_radio_normal"/>
</selector>

引用
<CheckBox
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:button="@drawable/check_demo" />
layer-list 标签

阴影效果,重要!

进度条背景进度的改变
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 注意是背景图在前 -->
    <item
        android:id="@android:id/background"
        android:drawable="@drawable/progress_bar_bg"/>
    
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/progress_bar_selected_bg"/>

</layer-list>

引用
   <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="80"
        style="?android:attr/progressBarStyleHorizontal"
        
        android:progressDrawable="@drawable/layer_demo" />

帧动画

选择 animation-list 标签
AndroidStudio下只有在 Drawable 文件夹下有选择。

<?xml version="1.0" encoding="utf-8"?>
<!-- 剧本,没有提示纯手写  oneshot设置只播放一次还是循环播放 -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true" >

    <!-- 每一帧需要播放什么图片 持续多少时间-->
    <item
        android:drawable="@drawable/girl_1"
        android:duration="200"/>

    <item
        android:drawable="@drawable/girl_2"
        android:duration="200"/>
</animation-list>

在控件引用
 <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@drawable/animation_demo" />
代码控制开启动画
private void AlertDemo() {
        btn = (Button) findViewById(R.id.btn);
        img = (ImageView) findViewById(R.id.img);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                
                //getDrawable()拿到图片src
                AnimationDrawable anim = (AnimationDrawable) img.getDrawable();
                anim.start();
            }
        });
    }

风格与主题

统一管理View的风格和系统的主题样式

在values下的styles操作

操作控件样式

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 将共同的样式抽取出来 -->
    <style name="BaseTheme">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <!-- parent继承抽取出来的样式 -->
    <style name="DemoTheme" parent="BaseTheme">
        <item name="android:text">@string/hello_world</item>
    </style>
</resources>

在Layout.xml中引用设定的样式
<TextView style="@style/DemoTheme" />
如果想修改App整体的样式风格

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 
  <!-- 自定义App整体风格,注意name的命名空间是android: -->
    <style name="CustomAppTheme">
        <item name="android:background">@android:color/darker_gray</item>
    </style>
</resources>

在AndroidManifest.xml的Theme中引用样式
<android:theme="@style/CustomAppTheme" />

AlertDialog

普通对话框

private void showDialog() {

        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("标题")
                .setMessage("提示消息")
                .setIcon(R.mipmap.ic_launcher_round)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setNeutralButton("其他", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .create();

        dialog.show();
    }
private void showDialog() {

        final String[] options = {"item1", "item2", "item3"};

        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("单选")
                .setSingleChoiceItems(options, 0, 
                        new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        
                        String item = options[which];
                        Toast.makeText(mContext, item, Toast.LENGTH_SHORT).show();
                    }
                })
                .create();

        dialog.show();
    }
private void showDialog() {

      final String[] options = {"item1", "item2", "item3"};
      final boolean[] isCheck = {true, false, false};

      AlertDialog dialog = new AlertDialog.Builder(this)
              .setTitle("多选")
              .setMultiChoiceItems(options, isCheck,
                      new DialogInterface.OnMultiChoiceClickListener() {
                          @Override
                          public void onClick(DialogInterface dialog, 
                                              int which, boolean isChecked) {
                                
                              isCheck[which] = isChecked;
                          }
                      })
              .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      String resCheck = "";
                      for (int i = 0; i < isCheck.length; i++) {
                          if (isCheck[i]) {
                              resCheck += options[i] + "\r\n";
                          }
                      }
                      Toast.makeText(mContext, resCheck, Toast.LENGTH_SHORT).show();
                  }
              })
              .create();

      dialog.show();
}
进度条对话框

    private void showDialog() {
        final ProgressDialog dialog = new ProgressDialog(this);
//        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//        dialog.setMax(100);
//        dialog.setProgress(100);
//        dialog.setSecondaryProgress(80);
//        dialog.setCancelable(false);
        dialog.setMessage("提示信息");
        dialog.show();
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                dialog.dismiss();
            }
        }, 2000);
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容