【Android基础入门〖15〗】Shape圆角输入框

来源:互联网 发布:键盘弹钢琴软件 编辑:程序博客网 时间:2024/05/17 02:03

目录(?)[+]

1 简介

本文主要介绍通过 shape 来设置 EditText 的圆角。
 

2 shape 的设置

 

shape_life_search.xml 放在 res/drawable 文件夹内

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <!-- 角度 -->  
  4.     <corners android:radius="100dp"/>  
  5.     <!-- 填充色 -->  
  6.     <solid android:color="#ffffff"/>  
  7.     <!-- 描边 设置线宽及颜色 -->  
  8.     <stroke android:color="#cccacb"  
  9.         android:width="1dp"/>  
  10. </shape>  


3 shape 的使用

 
在 activity_main.xml 主布局中如此使用

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:background="#f5f5f5"  
  7.     tools:ignore="HardcodedText,ContentDescription" >  
  8.       
  9.     <!-- Head start -->  
  10.     <LinearLayout  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:orientation="horizontal"  
  14.         android:padding="10dp"  
  15.         android:background="#ff5a54">  
  16.         <ImageView  
  17.             android:layout_width="wrap_content"  
  18.             android:layout_height="match_parent"  
  19.             android:src="@drawable/head_left" />  
  20.         <TextView  
  21.             android:layout_width="0dp"  
  22.             android:layout_height="match_parent"  
  23.             android:layout_weight="1"  
  24.             android:gravity="center"  
  25.             android:text="周边生活"  
  26.             android:textStyle="bold"  
  27.             android:textSize="20sp"  
  28.             android:textColor="@android:color/white"/>  
  29.         <ImageView  
  30.             android:layout_width="wrap_content"  
  31.             android:layout_height="match_parent"  
  32.             android:src="@drawable/head_right" />  
  33.     </LinearLayout>  
  34.     <!-- Head end   -->  
  35.     <!-- Search start-->  
  36.     <LinearLayout  
  37.         android:layout_width="match_parent"  
  38.         android:layout_height="wrap_content"  
  39.         android:orientation="vertical"   
  40.         android:layout_margin="10dp">  
  41.         <EditText  
  42.             android:id="@+id/search_edit"  
  43.             android:layout_width="match_parent"  
  44.             android:layout_height="wrap_content"  
  45.             android:drawableLeft="@drawable/search_left"  
  46.               android:background="@drawable/shape_life_search"  
  47.               android:hint="请输入商户或地址"  
  48.             android:padding="10dp"  
  49.             android:textColorHint="#b7b7b7"  
  50.             android:textStyle="bold"/>  
  51.     </LinearLayout>  
  52.     <!-- Search end  -->  
  53.     <LinearLayout  
  54.         android:layout_width="match_parent"  
  55.         android:layout_height="wrap_content"  
  56.         android:orientation="vertical" >  
  57.         <ImageView  
  58.             android:layout_width="match_parent"  
  59.             android:layout_height="match_parent" />  
  60.     </LinearLayout>  
  61. </LinearLayout>  


4 用到的资源

 
head_left.png

head_right.png

search_left.png

 
 

5 隐藏标题栏

 
  1. package com.ui;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.Window;  
  5. public class MainActivity extends Activity {  
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.           
  10.         //隐藏应用程序标题栏    (能看到手机通知栏)  
  11.          requestWindowFeature(Window.FEATURE_NO_TITLE);                                                                               
  12.         //设置全屏            (手机通知栏也被隐藏)  
  13.         //this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  14.           
  15.         setContentView(R.layout.activity_main);  
  16.     }  
  17. }  

6 结果预览

 
注:在 eclipse 的 layout 布局预览功能中,圆角不是很明显,但是安装到模拟器后就正常显示了,具体原因未知。
 
在 eclipse 中预览结果
 
在模拟器中的结果
 






 注:转载请注明出处 :)   毕竟代码是一个一个敲出来的啊,O(∩_∩)O~
http://blog.csdn.net/mkrcpp/article/details/12036463