Android 개발 튜 토리 얼 의 shape 와 selector 의 결합 사용
1.Shape
간단 한 소개
역할:XML 에서 정의 하 는 기하학 적 모양
위치:res/drawable/파일 이름.xml
사용 방법:
자바 코드 중:R.drawable.파일 이름
XML:android:background="@drawable/파일 이름"
속성:
그 중에서 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>
첫 번 째 예:원 각 의 Buttonhttp://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 의 결합 사용 에 관 한 내용 이 므 로 도움 이 되 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.