1. 程式人生 > >Android 開發筆記___shape

Android 開發筆記___shape

mco ext cor tex 分享 width eat XML super

shape_oval

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:shape="oval" >
 4 
 5     <solid android:color="#ff66aa" />
 6 
 7     <stroke
 8         android:width="1dp"
 9         android:color="#ffaaaaaa" />
10 11 </shape>

技術分享

默認矩形

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <shape xmlns:android="http://schemas.android.com/apk/res/android" >
 3 
 4     <solid android:color="#ffdd66" />
 5 
 6     <stroke
 7         android:width="1dp"
 8         android:color="#ffaaaaaa" />
 9 
10
<corners 11 android:bottomLeftRadius="10dp" 12 android:bottomRightRadius="10dp" 13 android:topLeftRadius="10dp" 14 android:topRightRadius="10dp" /> 15 16 </shape>

技術分享

xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <View 8 android:id="@+id/v_content" 9 android:layout_width="match_parent" 10 android:layout_height="200dp" 11 android:layout_margin="20dp" /> 12 13 <LinearLayout 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:orientation="horizontal"> 17 18 <Button 19 android:id="@+id/btn_rect" 20 android:layout_width="0dp" 21 android:layout_height="wrap_content" 22 android:layout_weight="1" 23 android:text="圓角矩形背景" 24 android:textColor="#000000" 25 android:textSize="17sp" /> 26 27 <Button 28 android:id="@+id/btn_oval" 29 android:layout_width="0dp" 30 android:layout_height="wrap_content" 31 android:layout_weight="1" 32 android:text="橢圓背景" 33 android:textColor="#000000" 34 android:textSize="17sp" /> 35 36 </LinearLayout> 37 38 </LinearLayout>

java

 1 package com.example.alimjan.hello_world;
 2 
 3 import android.content.Context;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.support.v7.app.AppCompatActivity;
 7 import android.view.View;
 8 import android.widget.Button;
 9 
10 /**
11  * Created by alimjan on 7/1/2017.
12  */
13 
14 public class class__2_4_3 extends AppCompatActivity implements View.OnClickListener {
15 
16     private View v_content;
17 
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.code_2_4_3);
22         v_content = (View) findViewById(R.id.v_content);
23 
24         Button btn_rect = (Button) findViewById(R.id.btn_rect);
25         Button btn_oval = (Button) findViewById(R.id.btn_oval);
26         btn_rect.setOnClickListener(this);
27         btn_oval.setOnClickListener(this);
28     }
29 
30     @Override
31     public void onClick(View v) {
32         if (v.getId() == R.id.btn_rect) {
33             v_content.setBackgroundResource(R.drawable.shape_rect_gold);
34         } else if (v.getId() == R.id.btn_oval) {
35             v_content.setBackgroundResource(R.drawable.shape_oval_rose);
36         }
37     }
38 
39     public static void startHome(Context mContext) {
40         Intent intent = new Intent(mContext, class__2_4_3.class);
41         mContext.startActivity(intent);
42     }
43 
44 }

技術分享技術分享

1、shape 有四種類型

  rectangle  矩形

  oval    橢圓(corners屬性失效)

  line    直線(必須設置stroke屬性)

  ring    圓環

2、corners

  bottomLeftRadius:左下角

  bottomRightRadius:右下角

  topLeftRadius:左上角

  topRightRadius:右上角

3、gradien(顏色漸變)

  linear:線性漸變

  radial:放射漸變

  sweep:滾動漸變

4、padding 間隔大小

5、size  圖形尺寸大小

6、solid  內部填充顏色

7、stroke 四周變現

  color:

  dashGap:每段虛線之間間隔

  dashWidth:每段虛線寬度

  width:描邊的厚度

Android 開發筆記___shape