Android 개발 튜 토리 얼 의 shape 와 selector 의 결합 사용

8614 단어 androidshapeselector
shape 와 selector 는 Android UI 디자인 에서 자주 사용 되 는 것 입 니 다.예 를 들 어 우 리 는 원 각 Button 을 사용자 정의 하고 Button 을 누 르 면 효과 적 인 변화 가 있 으 면 shape 와 selector 를 사용 해 야 합 니 다.이렇게 말 하면 shape 와 selector 는 컨트롤 을 미화 하 는 데 중요 한 역할 을 한다.
1.Shape
간단 한 소개
역할:XML 에서 정의 하 는 기하학 적 모양
위치:res/drawable/파일 이름.xml
사용 방법:
자바 코드 중:R.drawable.파일 이름
XML:android:background="@drawable/파일 이름"
속성:
android:shape=["rectangle" | "oval" | "line" | "ring"]
그 중에서 rectagle 사각형,oval 타원,line 수평 직선,링 링 링
중성자 노드 의 상용 속성:
그 라 데 이 션
android:startColor 시작 색상
android:endColor 끝 색상
안 드 로 이 드:angle 그 라 데 이 션 각도,0 은 위 에서 아래로,90 은 왼쪽 에서 오른쪽으로,수 치 는 45 의 정수 배 기본 값 은 0 입 니 다.
android:type 그 라 데 이 션 스타일 liner 선형 그 라 데 이 션 radial 링 그 라 데 이 션 sweep
충전
android:color 가 채 워 진 색
테두리 그리 기
android:width 테두리 너비
android:color 테두리 색상
android:dashWidth 는'-'가로줄 의 폭'을 표시 합 니 다.
android:dashGap 은'-'횡선 사이 의 거 리 를 표시 합 니 다.
원 각
android:radius 원 각 의 반지름 값 이 클 수록 원 입 니 다.
오른쪽 상단 원 각 반경
android:bottomLeft Radius 오른쪽 아래 원 각 반경
왼쪽 상단 원 각 반경
android:bottomRightRadius 왼쪽 아래 원 각 반경
2.Selector
간단 한 소개
위치:res/drawable/파일 이름.xml
사용 방법:
자바 코드 중:R.drawable.파일 이름
XML:android:background="@drawable/파일 이름"

<?xml version="1.0" encoding="utf-8" ?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<!--         --> 
<item android:drawable="@drawable/pic1" /> 
<!--            --> 
<item 
android:state_window_focused="false" 
android:drawable="@drawable/pic_blue" 
/> 
<!--                     --> 
<item 
android:state_focused="true" 
android:state_pressed="true" 
android:drawable= "@drawable/pic_red" 
/> 
<!--              --> 
<item 
android:state_focused="false" 
android:state_pressed="true" 
android:drawable="@drawable/pic_pink" 
/> 
<!--        --> 
<item 
android:state_selected="true" 
android:drawable="@drawable/pic_orange" 
/> 
<!--          --> 
<item 
android:state_focused="true" 
android:drawable="@drawable/pic_green" 
/> 
</selector> 
첫 번 째 예:원 각 의 Button
http://liangruijun.blog.51cto.com/3061169/630051
두 번 째 예:shape+selector 를 종합 적 으로 사용 한 예 쁜 ListView
먼저 이 예 의 구 조 를 살 펴 보 자.

selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_selected="true"> 
<shape> 
<gradient android:angle="270" android:endColor="#99BD4C" 
android:startColor="#A5D245" /> 
<size android:height="60dp" android:width="320dp" /> 
<corners android:radius="8dp" /> 
</shape> 
</item> 
<item android:state_pressed="true"> 
<shape> 
<gradient android:angle="270" android:endColor="#99BD4C" 
android:startColor="#A5D245"/> 
<size android:height="60dp" android:width="320dp" /> 
<corners android:radius="8dp" /> 
</shape> 
</item> 
<item> 
<shape> 
<gradient android:angle="270" android:endColor="#A8C3B0" 
android:startColor="#C6CFCE" /> 
<size android:height="60dp" android:width="320dp" /> 
<corners android:radius="8dp" /> 
</shape> 
</item> 
</selector> 
list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/selector" 
> 
<ImageView 
android:id="@+id/img" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center_vertical" 
android:layout_marginLeft="20dp" 
/> 
<TextView 
android:text="data" 
android:id="@+id/title" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:gravity="center_vertical" 
android:layout_marginLeft="20dp" 
android:layout_marginTop="20dp" 
android:textSize="14sp" 
android:textStyle="bold" 
android:textColor="@color/black" 
> 
</TextView> 
</LinearLayout>
main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="#253853" 
> 
<ListView 
android:id="@+id/list" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:cacheColorHint="#00000000" 
android:divider="#2A4562" 
android:dividerHeight="3px" 
android:listSelector="#264365" 
android:drawSelectorOnTop="false" 
> 
</ListView> 
</LinearLayout> 
colors.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<color name="white">#FFFFFFFF</color> 
<color name="transparency">#00000000</color> 
<color name="title_bg">#1C86EE</color> 
<color name="end_color">#A0cfef83</color> 
<color name="black">#464646</color> 
</resources> 
MainActivity.xml

package com.lingdududu.customlist; 
import java.util.ArrayList; 
import java.util.HashMap; 
import xb.customlist.R; 
import android.R.array; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
public class MainActivity extends Activity { 
ListView list; 
String data[] = new String[]{ 
"China","UK","USA","Japan","German","Canada","ET","Narotu" 
}; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
list =(ListView) findViewById(R.id.list); 
SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.list_item, 
new String[]{"title","img"}, new int[]{R.id.title,R.id.img}); 
list.setAdapter(adapter); 
} 
private ArrayList<HashMap<String, Object>> getData() { 
ArrayList<HashMap<String, Object>> dlist = new ArrayList<HashMap<String,Object>>(); 
for(int i =0;i<data.length;i++){ 
HashMap<String, Object>map = new HashMap<String, Object>(); 
map.put("title", data[i]); 
map.put("img", R.drawable.item_left2); 
dlist.add(map); 
} 
return dlist; 
} 
}

효과 그림:

위 와 같이 소 편 이 공유 하 는 안 드 로 이 드 개발 튜 토리 얼 의 shape 와 selector 의 결합 사용 에 관 한 내용 이 므 로 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기