Android布局中实现圆角边框

来源:互联网 发布:域名绑定记录类型 编辑:程序博客网 时间:2024/05/16 06:30
您的评价:
     
 收藏该经验    

设置corners_bg.xml

设置边框圆角可以在drawable-mdpi目录里定义一个xml: 

?
1
2
3
4
5
6
7
8
<?xmlversion="1.0"encoding="utf-8"?> 
<shapexmlns:android="http://schemas.android.com/apk/res/android">   
    <solidandroid:color="#FFFFFF"/>   
    <cornersandroid:topLeftRadius="10dp"  
             android:topRightRadius="10dp"   
             android:bottomRightRadius="10dp"  
             android:bottomLeftRadius="10dp"/>   
</shape>
解释:

solid的表示填充颜色,为了简单,这里用的是白色。 

而corners则是表示圆角,注意的是这里bottomRightRadius是左下角而不是右下角,bottomLeftRadius右下角。 


上面的效果也可以像下面一样设置,如下:

?
1
<cornersandroid:radius="5dp"/>

回到顶部

引用corners_bg.xml

如果想引用这个xml,只需要@drawable/corners_bg.xml即可: 

?
1
android:background="@drawable/corners_bg"

回到顶部

应用范例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E1E0DE"
    android:orientation="vertical">
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#2B3439"
        android:gravity="center"
        android:text="发现"
        android:textColor="#FFFFFF"
        android:textSize="20sp"/>
 
    <LinearLayout
        android:id="@+id/login_div"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:background="@drawable/corners_bg"
        android:gravity="center_vertical"
        android:padding="10dp">
 
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/find_more_friend_photograph_icon"/>
 
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14dp"
            android:text="朋友圈"
            android:textColor="#000"
            android:textSize="18sp"/>
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="101dp"
        android:layout_margin="10dp"
        android:background="@drawable/corners_bg"
        android:orientation="vertical"
        android:padding="10dp">
 
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="50dp">
 
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/find_more_friend_scan"/>
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="14dp"
                android:text="扫一扫"
                android:textColor="#000"
                android:textSize="18sp"/>
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_centerVertical="true">
 
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/come_from_shake"/>
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="14dp"
                android:text="摇一摇"
                android:textColor="#000"
                android:textSize="18sp"/>
        </LinearLayout>
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="101dp"
        android:layout_margin="10dp"
        android:background="@drawable/corners_bg"
        android:orientation="vertical"
        android:padding="10dp">
 
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="50dp">
 
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/find_more_friend_near_icon"/>
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="14dp"
                android:text="附近的人"
                android:textColor="#000"
                android:textSize="18sp"/>
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_centerVertical="true">
 
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/come_from_bottle"/>
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="14dp"
                android:text="漂流瓶"
                android:textColor="#000"
                android:textSize="18sp"/>
        </LinearLayout>
    </LinearLayout>
 
    <LinearLayout
        android:id="@+id/login_div"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:background="@drawable/corners_bg"
        android:gravity="center_vertical"
        android:padding="10dp">
 
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/more_game"/>
 
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14dp"
            android:text="游戏中心"
            android:textColor="#000"
            android:textSize="18sp"/>
    </LinearLayout>
 
</LinearLayout>

回到顶部

效果图

20130921004013046.jpeg

来自:http://blog.csdn.net/jueblog/article/details/11857479

0 0