Android 개발 에 include 컨트롤 용법 분석

이 사례 는 안 드 로 이 드 개발 에서 include 컨트롤 의 용법 을 다 루 었 다.여러분 께 참고 하도록 공유 하 겠 습 니 다.구체 적 으로 는 다음 과 같 습 니 다.
우 리 는 안 드 로 이 드 시스템 을 바탕 으로 하 는 응용 프로그램의 개발 은 인터페이스 디자인 이 매우 중요 하고 사용자 체험 의 좋 고 나 쁨 과 관련 이 있다 는 것 을 안다.좋 은 인터페이스 디자인 은 xml 레이아웃 으로 해결 할 수 있 는 것 이 아 닙 니 다.activity 에 컨트롤 이 매우 많 을 때 모든 레이아웃 파일 을 xml 파일 에 넣 으 면 얼마나 나 쁜 일 인지 상상 하기 쉽 습 니 다!필 자 는 자신의 경험 을 통 해 include 컨트롤 로 이 문 제 를 해결 했다.다음은 작은 예 이다.구조 만 실현 하고 코드 에 응 하지 않 는 디자인 이다.
user.xml 파일 내용 은 다음 과 같 습 니 다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal" >
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="   : " />
  <EditText
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:id="@+id/userName"
    android:hint="      "
    />
</LinearLayout>

passwd.xml 파일 내용 은 다음 과 같 습 니 다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal" >
   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="     :" />
  <EditText
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:id="@+id/passWd"
    android:hint="     "
    />
</LinearLayout>

login.xml 파일 내용 은 다음 과 같 습 니 다.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal" >
  <Button
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:id="@+id/bt"
    android:hint="  "
    />
  <Button
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:id="@+id/reset"
    android:layout_toRightOf="@id/bt"
    android:hint="  "
    />
</RelativeLayout>

main.xml 파일 내용 은 다음 과 같 습 니 다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:layout_alignParentBottom="true">
<RelativeLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:layout_alignParentBottom="true">
 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/head"
  android:layout_alignParentTop="true">
   <include
   android:layout_width="fill_parent"
   layout="@layout/user">
   </include>
 </LinearLayout>
 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/middle"
  android:layout_below="@id/head"
  android:layout_alignParentLeft="true">
   <include
   android:layout_width="fill_parent"
   layout="@layout/passwd">
   </include>
  </LinearLayout>
  <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/foot"
  android:layout_below="@id/middle"
  android:layout_alignParentRight="true">
   <include
   android:layout_width="fill_parent"
   layout="@layout/login">
   </include>
   </LinearLayout>
</RelativeLayout>
</LinearLayout>

프로그램 실행 결 과 는 다음 과 같 습 니 다.

main.xml 에 이렇게 쓰 면:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:layout_alignParentBottom="true">
   <include
   android:layout_width="fill_parent"
   layout="@layout/user">
   </include>
   <include
   android:layout_width="fill_parent"
   layout="@layout/passwd">
   </include>
   <include
   android:layout_width="fill_parent"
   layout="@layout/login">
   </include>
</LinearLayout>

그러면 이 상황 에서 의 운행 결 과 는 다음 과 같다.

실행 결과 가 예상 과 맞지 않 는 것 이 분명 합 니 다.다음 네 개의 컨트롤 이 나 오지 않 습 니 다.왜 일 까요?틀림없이 모두 가 실험 을 할 때,틀림없이 이 문제 에 부 딪 힌 적 이 있 을 것 이다.)
사실 관건 적 인 부분 은 main 파일 에서 각 xml 에 대한 레이아웃 입 니 다.해당 하 는 레이아웃 이 없 으 면 결 과 는 매우 비참 합 니 다.여러분 은 제 코드 에 따라 해당 하 는 레이아웃 을 수정 하고 main 에서 레이아웃 의 중요성 을 느 낄 수 있 습 니 다!
더 많은 안 드 로 이 드 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 고 에서 말 한 것 이 여러분 의 안 드 로 이 드 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기