를 사용하여 아래
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);
스냅
편집 :
당신이
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);
이 방법을 사용하면 중첩 될 relativeLayout (cs) 및 일부 추가 루트 레이아웃 (mylayout)을 갖게 될 필요가없는 여분의 중첩 수준이 생깁니다. 그들 중 하나는 쓸모가 없다. – stoefln
@stoefln 그래서 나는 당신의 질문을 더 명확히하거나 일부 코드 스 니펫을 게시하여 지금까지 해왔 던 것을 오해했을 수도 있습니다. 그들 중 하나는 쓸모가 없다.사실, 당신은 동적으로 텍스트 뷰를 생성 할 수 있고 그것들을 mylayout을 필요로하지 않는 커스텀 상대 레이아웃에 추가 할 수 있습니다. – Raghunandan
@stoefln'CustomRelativeLayout cs = new CustomRelativeLayout (this);'이제'TextView tv = new TextView (이);tv.setText ("hello");'이제'cs.addView (tv);'와 같은 상대적 레이아웃에 textview를 추가하십시오. mylayout 필요 없음 – Raghunandan