Programing

하나의 뷰에 레이아웃을 부 풀리는 방법

lottogame 2020. 4. 29. 08:08
반응형

하나의 뷰에 레이아웃을 부 풀리는 방법


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에 자식보기를 첨부하려고합니까? 그렇다면 다음 줄을 따라 무언가를 원합니다.

RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);

XML 리소스를 팽창시킵니다. LayoutInflater 문서를 참조하십시오 .

레이아웃이 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 = null;
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.mylayout, null);
main.addView(view);

활동이 아닌 경우 클래스 의 정적 from()메소드를 사용하여 LayoutInflater를 얻 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 년 전이지만 여전히 언급 할 가치가 있음을 알고 있습니다)


단일보기를 여러 번 추가하려면 다음을 사용해야합니다

   layoutInflaterForButton = getActivity().getLayoutInflater();

 for (int noOfButton = 0; noOfButton < 5; noOfButton++) {
        FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null);
        btnContainer.addView(btnView);
    }

If you do like

   layoutInflaterForButton = getActivity().getLayoutInflater();
    FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null);

and

for (int noOfButton = 0; noOfButton < 5; noOfButton++) {
            btnContainer.addView(btnView);
        }

then it will throw exception of all ready added view.


AttachToRoot Set to True

Just think we specified a button in an XML layout file with its layout width and layout height set to 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>

On This Buttons Click Event We Can Set Following Code to Inflate Layout on This Activity.

LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.yourlayoutname, this);

Hope this solution works for you.!


I had the hardest time with this error, because of my unique circumstances, but finally found a solution.

My situation: I am using a separate view (XML) which holds a WebView, then opens in an AlertDialog when I click a button in my main activity view. But somehow or another the WebView belonged to the main activity view (probably because I pull the resource from here), so right before I assigned it to my AlertDialog (as a view), I had to get the parent of my WebView, put it into a ViewGroup, then remove all the views on that ViewGroup. This worked, and my error went away.

// 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...

.... later on after I loaded my 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();

If you are you trying to attach a child view to the RelativeLayout? you can do by following

RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, item, true);

Try this code :

  1. If you just want to inflate your layout :

View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,null); // Code for inflating xml layout
RelativeLayout item = view.findViewById(R.id.item);   

  1. If you want to inflate your layout in container(parent layout) :

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.


With Kotlin, you can use:

val content = LayoutInflater.from(context).inflate(R.layout.[custom_layout_name], null)

[your_main_layout].apply {
    //..
    addView(content)
}

I had used below snippet of code for this and it worked for me.

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
linearLayout.addView(child);

No need any complexity it just simple as

 setContentView(R.layout.your_layout);

참고URL : https://stackoverflow.com/questions/2335813/how-to-inflate-one-view-with-a-layout

반응형