Android 사용자 정의 그림 추가 기능 구현

Android 사용자 정의 그림 추가 기능 구현
4 단계 로 나 누 어 쓰기:
1.조합 컨트롤 의 xml;
2.조합 컨트롤 의 속성 을 사용자 정의 합 니 다.
3.조합 레이아웃 을 계승 하 는 class 류 를 사용자 정의 하여 두 매개 변 수 를 가 진 구조 기 를 실현 합 니 다.
4.xml 에서 조합 컨트롤 을 보 여 줍 니 다.
구체 적 인 실현 과정:
1.조합 컨트롤 의 xml
내 가 접촉 하 는 두 가지 방식 이 있 는데 하 나 는 일반적인 Activity 의 xml 이다.하 나 는 부모 노드 가 merge 인 xml 입 니 다.제 프로젝트 에서 사용 하 는 것 은 첫 번 째 이지 만 개인 적 으로 두 번 째 가 좋 은 것 같 습 니 다.첫 번 째 가 상대 적 이거 나 절대적 인 구조 층 이 많 기 때 문 입 니 다.
내 가 쓴 custompictext.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <ImageView
    android:id="@+id/custom_pic_iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@mipmap/a" />

  <TextView
    android:id="@+id/custom_date_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/custom_pic_iv"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="8dp"
    android:text="2017" />

  <TextView
    android:id="@+id/custom_text_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/custom_pic_iv"
    android:layout_marginLeft="4dp"
    android:layout_marginTop="4dp"
    android:text="  " />
</RelativeLayout>

여기에 merge 의 예 를 보 여 줍 니 다.시간 이 있 으 면 여러분 이 직접 체험 할 수 있 습 니 다.

<merge xmlns:android="http://schemas.android.com/apk/res/android">

  <Button
    android:id="@+id/title_bar_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="5dp"
    android:background="@null"
    android:minHeight="45dp"
    android:minWidth="45dp"
    android:textSize="14sp" />

  <TextView
    android:id="@+id/title_bar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:singleLine="true"
    android:textSize="17sp" />

  <Button
    android:id="@+id/title_bar_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="7dp"
    android:background="@null"
    android:minHeight="45dp"
    android:minWidth="45dp"
    android:textSize="14sp" />

</merge>

이 두 xml 는 모두 layot 에 쓰 여 있 습 니 다.
2.조합 컨트롤 의 속성 을 사용자 정의 합 니 다.
이 단 계 는 사용자 정의 의 중요 한 부분 중 하나 입 니 다.사용자 정의 구성 요소 의 개인 적 인 특성 이 모두 여기에 표 시 됩 니 다.
우선 values 에 attrs.xml 를 만 듭 니 다.
그리고 속성 을 정의 합 니 다.다음 코드 입 니 다.

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
  <declare-styleable name="CustomPicText">
    <attr name="pic_backgroud" format="reference"/>
    <attr name="pic_backgroud_width" format="dimension"/>
    <attr name="pic_backgroud_height" format="dimension"/>
    <attr name="pic_text" format="string"/>
    <attr name="pic_text_color" format="color"/>
    <attr name="pic_text_size" format="integer"/>
    <attr name="pic_date" format="string"/>
    <attr name="pic_date_color" format="color"/>
    <attr name="pic_date_size" format="integer"/>
  </declare-styleable>

</resources>

여기 서 주의해 야 할 몇 가지 가 있 습 니 다.첫째,속성 명 은 name 이 고,둘째:속성 단 위 는 from at 입 니 다.이 단위 에 포 함 된 값 은 이곳 을 볼 수 있다.
3.조합 레이아웃 을 계승 하 는 class 류 를 사용자 정의 하여 두 매개 변 수 를 가 진 구조 기 를 실현 합 니 다.
내 가 구현 한 CustomPicText.Java

/**
 * Created by Hman on 2017/5/4.
 *            
 */
public class CustomPicText extends RelativeLayout {

  private ImageView customPicIv;
  private TextView customDateTv;
  private TextView customTextTv;

  public CustomPicText(Context context, AttributeSet attrs) {
    super(context, attrs);
    //   layout
    View view = LayoutInflater.from(context).inflate(R.layout.custom_pictext,this);
    customPicIv = (ImageView) view.findViewById(R.id.custom_pic_iv);
    customDateTv = (TextView) view.findViewById(R.id.custom_date_tv);
    customTextTv = (TextView) view.findViewById(R.id.custom_text_tv);

    //          
    TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.CustomPicText);
    //           
    if (typedArray != null) {
      //        
      int picBackgroud = typedArray.getResourceId(R.styleable.CustomPicText_pic_backgroud, 0);
      float picWidth = typedArray.getDimension(R.styleable.CustomPicText_pic_backgroud_width,25);
      float picHeight = typedArray.getDimension(R.styleable.CustomPicText_pic_backgroud_height,25);
      customPicIv.setBackgroundResource(picBackgroud);
//      customPicIv.setMinimumWidth(picWidth);

      //        
      String picText = typedArray.getString(R.styleable.CustomPicText_pic_text);
      int picTextColor = typedArray.getColor(R.styleable.CustomPicText_pic_text_color,16);
      int picTextSize = typedArray.getResourceId(R.styleable.CustomPicText_pic_date_size, 16);
      customTextTv.setText(picText);
      customTextTv.setTextColor(picTextColor);
      customTextTv.setTextSize(picTextSize);

      //        
      String picDate = typedArray.getString(R.styleable.CustomPicText_pic_date);
      int picDateColor = typedArray.getColor(R.styleable.CustomPicText_pic_date_color, 0);
      int picDateSize = typedArray.getResourceId(R.styleable.CustomPicText_pic_date_size, 12);
      customDateTv.setText(picDate);
      customDateTv.setTextColor(picDateColor);
      customDateTv.setTextSize(picDateSize);

      typedArray.recycle();


    }


  }
}

여기 서 우 리 는 컨트롤 에 감청 기 를 추가 할 수 있 고 모두 가 직접 추가 할 수 있다.여기 서 주의해 야 할 것 은 설정 을 불 러 오 는 클래스 TypeArray 입 니 다.
4.xml 에서 조합 컨트롤 을 보 여 줍 니 다.
이것 은 xml 에 마음대로 쓰 면 됩 니 다.
코드 는 다음 과 같다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:hman="http://schemas.android.com/apk/res-auto"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <com.eastsun.widget.CustomPicText
    android:id="@+id/first"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    hman:pic_backgroud="@mipmap/b"
    hman:pic_date="2017/5/6"
    hman:pic_date_color="@color/white"
    hman:pic_text="     "
    hman:pic_text_color="@color/red"
    hman:pic_text_size="18"></com.eastsun.widget.CustomPicText>

</LinearLayout>

여기 한 가지 잊 지 마 세 요.xmlns:hman=을 추가 합 니 다.http://schemas.Android.com/apk/res-auto”
총결산
절 차 는 기본적으로 여기까지 끝났다.
효과 캡 처 봐.

읽 어 주 셔 서 감사합니다. 여러분 에 게 도움 이 되 기 를 바 랍 니 다.본 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기