2013-12-15 2 views
2

많은 다른보기를 래핑하는 사용자 지정보기 (RelativeLayout을 확장)를 만들려고합니다. xml 레이아웃 파일에 해당 하위보기를 만들고 싶습니다. 이제 어떻게 레이아웃을 부 풀릴 수 있고 내 사용자 정의보기에서 사용할 수 있을지 궁금합니다. 이런 식으로 뭔가가 (내 사용자 정의보기 내부) 좋을 것 :사용자 지정보기 : 루트 레이아웃을 설정하는 방법

RelativeLayout rootLayout = (RelativeLayout) inflater.inflate(my xml file) 
this.setContenView(rootLayout); 

는 Unfortunatelly이 활동에서만 가능합니다. 보기와 비슷한 것이 있습니까?

편집 : 필자는 View.addView (rootLayout)를 사용하지 않으므로 다른보기 hierachy를 추가 할 필요가 없습니다. 보기 당신이 컨텍스트에서 레이아웃 인플레이터를 얻을 아이들을 팽창하고 this에 추가 할 수 있습니다 내

답변

2

당신은 루트로 <merge> 태그를 사용하려고 할 수있는 생성자에서 CustomRelative 레이아웃

에서 팽창하고자하는 경우 요소를 레이아웃에 넣고 맞춤형 RelativeLayout에으로 부 풀립니다. 210은 부모로, attachToRoot은 true로 설정됩니다. 그러면 addView 번으로 전화하지 않아도됩니다.

여기에 LinearLayout (페이지 아래쪽)과 비슷한 예제가 있는데, RelativeLayout too과 함께 사용해야합니다.

0

(RelativeLayout의 하위 클래스)

final LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
final View child = inflater.inflate(R.layout.custom_layout, this, false); 
// Then add child to this (subclass of RelativeLayout) 
this.addView(child); 

편집 :

위의 코드는 팽창하는 방법을 보여줍니다 사용자 정의보기 내부의 어린이. This link은 XML 레이아웃에 사용자 지정보기 자체를 삽입하는 방법을 보여줍니다.

1

를 사용하여 아래

View v =getLayoutInflater().inflate(R.layout.mylayout,null); 
// inflate mylayout.xml with other views 
CustomRelativeLayout cs = new CustomRelativeLayout(this); 
// CustomRelativeLayout is a class that extends RelativeLayout 
cs.addView(v); // add the view to relative layout 
setContentView(cs); // set the custom relative layout to activity 

예 :

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

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="111dp" 
     android:text="TextView" /> 

</RelativeLayout> 

SView MainActivtiy

에서

public class SView extends RelativeLayout { 

    Paint p,paint; 
    public SView(Context context) { 
     super(context); 
     TextView tv = new TextView(context); 
     tv.setText("hello"); 
     this.addView(tv); 
    } 
} 

View v =getLayoutInflater().inflate(R.layout.mylayout,null); 
SView cs = new SView(this); 
cs.addView(v); 
setContentView(cs); 

스냅

enter image description here

편집 :

당신이

LayoutInflater inflater = LayoutInflater.from(context); 
View v =inflater.inflate(R.layout.mylayout,null); 
TextView tv = new TextView(context); 
tv.setText("hello"); 
this.addView(tv); 
this.addView(v); 
+0

이 방법을 사용하면 중첩 될 relativeLayout (cs) 및 일부 추가 루트 레이아웃 (mylayout)을 갖게 될 필요가없는 여분의 중첩 수준이 생깁니다. 그들 중 하나는 쓸모가 없다. – stoefln

+0

@stoefln 그래서 나는 당신의 질문을 더 명확히하거나 일부 코드 스 니펫을 게시하여 지금까지 해왔 던 것을 오해했을 수도 있습니다. 그들 중 하나는 쓸모가 없다.사실, 당신은 동적으로 텍스트 뷰를 생성 할 수 있고 그것들을 mylayout을 필요로하지 않는 커스텀 상대 레이아웃에 추가 할 수 있습니다. – Raghunandan

+0

@stoefln'CustomRelativeLayout cs = new CustomRelativeLayout (this);'이제'TextView tv = new TextView (이);tv.setText ("hello");'이제'cs.addView (tv);'와 같은 상대적 레이아웃에 textview를 추가하십시오. mylayout 필요 없음 – Raghunandan

관련 문제