레이아웃으로 하나의 뷰를 부풀리는 방법
XML로 정의된 레이아웃이 있습니다. 또한 다음을 포함합니다.
<RelativeLayout
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
이 RelativeView를 다른 XML 레이아웃 파일로 확장하고 싶습니다.상황에 따라 다른 레이아웃을 사용할 수 있습니다.어떻게 하면 좋을까요?저는 다양한 변형을 시도하고 있었습니다.
RelativeLayout item = (RelativeLayout) findViewById(R.id.item);
item.inflate(...)
하지만 어느 것도 제대로 작동하지 않았습니다.
질문하신 내용을 잘 이해하지 못했습니다. 상대 레이아웃에 하위 보기를 첨부하시겠습니까?그렇다면 다음과 같은 작업을 수행해야 합니다.
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
XML 리소스를 확장합니다.레이아웃 인플레이터 문서를 참조하십시오.
레이아웃이 mylayout.xml에 있으면 다음과 같은 작업을 수행합니다.
View view;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
RelativeLayout item = (RelativeLayout) view.findViewById(R.id.item);
답변이 늦었지만, 이것을 얻기 위한 한 가지 방법을 추가하고 싶습니다.
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.mylayout, item );
어디에item
하위 레이아웃을 추가할 상위 레이아웃입니다.
여기에 오래된 게시물임에도 불구하고 xml에서 부풀려지는 하위 보기를 뷰 그룹 레이아웃에 추가하려면 추가할 뷰 그룹 유형에 대한 단서와 함께 부풀리기를 호출해야 합니다.예:
View child = getLayoutInflater().inflate(R.layout.child, item, false);
가압 방법은 상당히 오버로드되어 있으며 문서에서 이 부분을 설명합니다.이러한 유형의 변경을 수행하기 전까지는 xml에서 확장된 단일 뷰가 부모에서 제대로 정렬되지 않는 문제가 있었습니다.
훨씬 더 간단한 방법은
View child = View.inflate(context, R.layout.child, null)
item.addChild(child) //attach to your item
다음 코드를 사용해 보십시오.
- 레이아웃을 부풀리려는 경우:
View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,null); // Code for inflating xml layout
RelativeLayout item = view.findViewById(R.id.item);
- 컨테이너(상위 레이아웃)에서 레이아웃을 부풀리려는 경우:
LinearLayout parent = findViewById(R.id.container); //parent layout.
View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,parent,false);
RelativeLayout item = view.findViewById(R.id.item); //initialize layout & By this you can also perform any event.
parent.addView(view); //adding your inflated layout in parent layout.
배치 인플레이션
View view = null;
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
main.addView(view);
활동 중이 아닌 경우 정적 기능을 사용할 수 있습니다.from()
의 방법LayoutInflater
a를 받기 위한 수업LayoutInflater
또는 컨텍스트 메소드에서 서비스를 요청합니다.getSystemService()
너무 :
LayoutInflater i;
Context x; //Assuming here that x is a valid context, not null
i = (LayoutInflater) x.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//OR
i = LayoutInflater.from(x);
(거의 4년 전이라는 것을 알지만 여전히 언급할 가치가 있습니다.)
루트 집합에 True로 연결
XML 레이아웃 파일에서 레이아웃 너비와 레이아웃 높이가 match_parent로 설정된 버튼을 지정했다고 생각하면 됩니다.
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/custom_button">
</Button>
이 단추에서 이벤트를 클릭합니다. 이 활동의 레이아웃을 부풀리려면 다음 코드를 설정할 수 있습니다.
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.yourlayoutname, this);
이 솔루션이 귀하에게 효과가 있기를 바랍니다.!
단일 보기를 여러 번 추가하려면 다음을 사용해야 합니다.
layoutInflaterForButton = getActivity().getLayoutInflater();
for (int noOfButton = 0; noOfButton < 5; noOfButton++) {
FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null);
btnContainer.addView(btnView);
}
좋으시다면
layoutInflaterForButton = getActivity().getLayoutInflater();
FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null);
그리고.
for (int noOfButton = 0; noOfButton < 5; noOfButton++) {
btnContainer.addView(btnView);
}
그러면 이미 추가된 모든 보기에서 예외가 발생합니다.
상대 레이아웃에 하위 뷰를 첨부하려는 경우?당신은 다음과 같이 할 수 있습니다.
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, item, true);
저는 저의 독특한 환경 때문에 이 오류로 인해 가장 힘들었지만, 마침내 해결책을 찾았습니다.
situde: 는 지금 XML에는 XML이 들어 있습니다.WebView
그리고 나서 an에서 열립니다.AlertDialog
기본 활동 보기에서 단추를 누르면 됩니다.든 하지어떻든게만든▁the▁but게떻어▁or.WebView
기본 활동 보기에 속했습니다(아마도 리소스를 여기서 끌어오기 때문일 것입니다). 그래서 내가 그것을 내게 할당하기 직전에.AlertDialog
제 (관로서부제모을했모다셔습니야님저는으점▁of▁my▁parent다했▁the▁(니▁(습▁i),관▁get모▁to),셔▁a▁view▁hadas)의 부모님을 모셔야 했습니다.WebView
에 대해 설명합니다.ViewGroup
그런 다음 그것에 대한 모든 견해를 제거합니다.ViewGroup
이것은 효과가 있었고, 저의 실수는 사라졌습니다.
// set up Alert Dialog box
AlertDialog.Builder alert = new AlertDialog.Builder(this);
// inflate other xml where WebView is
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View v = layoutInflater.inflate(R.layout.your_webview_layout, null);
final WebView webView = (WebView) v.findViewById(R.id.your_webview_id);
// more code...
나중에 로드한 후에WebView
....
// first, remove the parent of WebView from it's old parent so can be assigned a new one.
ViewGroup vg = (ViewGroup) webView.getParent();
vg.removeAllViews();
// put WebView in Dialog box
alert.setView(webView);
alert.show();
Kotlin을 사용하면 다음을 사용할 수 있습니다.
val content = LayoutInflater.from(context).inflate(R.layout.[custom_layout_name], null)
[your_main_layout].apply {
//..
addView(content)
}
Kotlin의 활동에 레이아웃을 추가할 때 다음 단계를 참조하십시오.
활동에만 추가 - 상위로 하나의 레이아웃
새 XML 파일
레이아웃 R의 값을 제공합니다.
유효: 선형 레이아웃 = findViewById(R.id .stars)
val view = LayoutInflater.from(applicationContext).inflate(R.layout.another, parent,false)
에▁where디parent
필요하지 않습니다, 될 수 있습니다.null
경고 것입니다.
view.findViewById<ImageView>(R.id.ivTimer).setImageResource(R.drawable.t2)
모든 뷰는 이러한 방식으로 값을 설정해야 합니다. 마지막으로add
parent.apply { addView(view)}
저는 이것을 위해 아래의 코드 조각을 사용했고 그것은 저에게 효과가 있었습니다.
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
linearLayout.addView(child);
언급URL : https://stackoverflow.com/questions/2335813/how-to-inflate-one-view-with-a-layout
'programing' 카테고리의 다른 글
Unix 새 줄에서 Windows 새 줄로(Windows의 경우 (0) | 2023.08.04 |
---|---|
JavaScript에서 날짜를 MM/dd/yyyy로 형식 지정 (0) | 2023.08.04 |
PL/SQL에 대한 코드 적용 범위 (0) | 2023.08.04 |
MariaDB prepareStatement가 where 절에 대한 characterSet을 변환하지 않습니다. (0) | 2023.08.04 |
SQL 열에서 문자의 인스턴스 수를 계산하는 방법 (0) | 2023.08.04 |