2012-01-26 3 views
2

뷰 그룹을 뷰그 그룹 내에 배치하는 것이 좋습니다.
나는 setLayoutParams() 메소드를 사용하려했지만 viewstubs에는 사용할 수 없습니다.
해결책을 제안하십시오.뷰 스텁을 프로그래밍 방식으로 배치하십시오.

아래 코드는 제 코드입니다.

setContentView(R.layout.textplay); 
bt = (Button) findViewById(R.id.button1); 

bt.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       panel = (ViewStub) findViewById(R.id.serachViewStub2); 
       panel.inflate(getApplicationContext(), R.layout.half_screen,null); 
       panel.setVisibility(View.VISIBLE);  
       panel.setOnInflateListener(new OnInflateListener() { 

        @Override 
        public void onInflate(ViewStub arg0, View arg1) { 
         // TODO Auto-generated method stub 

         parentLayout = (LinearLayout) findViewById(R.id.stubLayout1); 
         parentLayout.setLayoutParams(new LinearLayout.LayoutParams(0, 300)); 

      } 
     }); 
        } 
     }); 

halfscreen.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/stubLayout1" android:layout_width="fill_parent" 
    android:layout_height="50dp" android:weightSum="100" 
    android:padding="2dp" 
    android:orientation="horizontal" android:background="@color/white"> 
    <EditText android:id="@+id/editText1" android:layout_height="40dp" 
     android:layout_width="fill_parent" android:layout_weight="10" 
     android:hint="Search Location" android:paddingTop="2dp"> 
     <requestFocus></requestFocus> 
    </EditText> 
    <Button android:id="@+id/search_button" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:layout_weight="90" 
     android:background="@drawable/custom_search_button"> 
    </Button> 
</LinearLayout> 

textplay.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="25dp" 
    android:paddingTop="25dp" android:paddingRight="25dp" android:background="@drawable/back2"> 
    <EditText android:layout_height="wrap_content" 
     android:layout_width="match_parent" android:id="@+id/editText1" 
     android:hint="Type a Command"> 
     <requestFocus></requestFocus> 
    </EditText> 
    <LinearLayout android:layout_height="wrap_content" 
     android:layout_width="match_parent" android:id="@+id/linearLayout1" 
     android:weightSum="100"> 
     <Button android:id="@+id/button1" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:layout_weight="90" 
      android:text="Try Command"></Button> 
     <ToggleButton android:text="ToggleButton" android:id="@+id/toggleButton1" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" 
      android:paddingBottom="10dp" android:layout_weight="10"></ToggleButton> 
    </LinearLayout> 
    <TextView android:id="@+id/textView1" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:text="Invalid" 
     android:gravity="center" android:layout_gravity="center"></TextView> 
<ViewStub android:id="@+id/serachViewStub2" android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:inflatedId="@+id/search_stub" 
      android:layout="@layout/half_screen"> 
     </ViewStub> 

</LinearLayout> 

답변

2

당신이 ViewStub을 팽창 후에는 ViewStub 실제로 보여주는 레이아웃에 액세스 할 수 있습니다. 레이아웃에 대한 참조 및 setLayoutparameters를 가져옵니다.

ViewStub는 id가있는 LinearLayout이 mainContainer로 포함 된 my_layout.xml을 확장합니다. viewstub을 부 풀리면 mainContainer에 대한 참조를 얻고 레이아웃 매개 변수를 적용하십시오.

+0

dcanh121에 감사드립니다.하지만 여전히 viewstub을 배치 할 수 없습니다. 나는 코드를 포함 시켰습니다. 제발 수정을 제안 해주세요. –

0

나는 다음과 같은 방법으로 시도해 볼 수 있습니다. viewstub을 부 풀린 다음 해당 id로 뷰 스텁의 레이아웃을 가져온 다음 원하는 위치로 설정할 수 있습니다.

//mViewStub is the viewstub 
mViewStub.setVisibility(View.VISIBLE); 
//view is parent of viewstub, 
View viewstublayout = view.findViewById(R.id.viewstub_layout); 
if (viewstublayout != null) { 
    viewstublayout.layout(l, t, r, b); 
} 

은 참고하시기 바랍니다

1

ViewStubRelativeLayout 컨테이너 아래에, 당신은 레이아웃 매개 변수를 설정하려면 다음 코드를 사용할 수 있습니다 가정하자.

View view = ((ViewStub) findViewById(R.id.view_stub)).inflate(); 
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams(); 
params.bottomMargin = 0; 
params.topMargin = 50; 
view.setLayoutParams(params);