2011-01-25 9 views
14

레이아웃을 다른 레이아웃에 추가하는 방법 .tblrow.XML.so에이 레이아웃을 만들었습니다.이 행을 menu.XML.i에 추가하고 싶습니다. 내가 어떻게 할 수 있겠 어. 내가 추가하면 각 행을 어떻게 식별 할 수 있을까? 제발 도와주세요. XML이 아닌 java에서 솔루션이 필요합니다. 내 코드는다른 레이아웃의 레이아웃 추가

<TableRow android:id="@+id/tblRowMovies" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/btn_backgrnd" android:clickable="true" android:focusable = "true" android:layout_weight="1.0"> 
<ImageView android:layout_width="wrap_content" android:layout_height="fill_parent" android:src="@drawable/movie" android:layout_gravity="center_vertical|center_horizontal"/> 
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="MOVIES" android:layout_gravity="center_vertical" android:paddingLeft="20dp" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold"> 
</TextView>   

입니다.

답변

26

레이아웃 파일을 다른 레이아웃 파일에 포함 할 수 있으며이 기술을 include 태그를 사용하여 Android 레이아웃 재사용이라고합니다. 예컨대 :

<include android:id="@+id/myid1" layout="@layout/workspace_screen" /> 

a blogpost by Android developers.에서 잘 설명되어 있습니다.

또 다른 기사는 Android developer site explains the layout re-usability입니다.

+0

내가 이걸 그리고 자바 코드에서 필요 알고있다. –

+0

도움이 될만한 정보가 있습니다. http://stackoverflow.com/questions/3195668/android-programmatically-include-layout-i-e-without-xml – Gopinath

4

메인 레이아웃에서 파일 titlebar.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width=”match_parent” 
    android:layout_height="wrap_content" 
    android:background="@color/titlebar_bg"> 

    <ImageView android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/gafricalogo" /> 
</FrameLayout> 

만들기 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width=”match_parent” 
    android:layout_height=”match_parent” 
    android:background="@color/app_bg" 
    android:gravity="center_horizontal"> 

    <include layout="@layout/titlebar"/> 

    <TextView android:layout_width=”match_parent” 
       android:layout_height="wrap_content" 
       android:text="@string/hello" 
       android:padding="10dp" /> 

    ... 

</LinearLayout> 
관련 문제