2013-02-06 6 views
0

다른 레이아웃에 linearlayout을 추가하는 방법을 어떻게 만들 수 있는지 알고 싶습니다. 이 메서드를 액티비티에 사용할 때이 메서드는 선형 레이아웃을 내 액티비티 레이아웃 위에 추가하는 메서드를 만들고 싶습니다. 어떻게해야합니까?메소드 안드로이드에 레이아웃 추가하기

편집 : 나는 그런 일을 수행

public class SecondActivity extends Activity { 

    LinearLayout layout; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.second_activity); 

     layout = (LinearLayout) findViewById(R.id.lay); 
     Button next = (Button) findViewById(R.id.nextActivity); 
     layout.addView(addNewLinearLayout(getApplicationContext())); 

     next.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(SecondActivity.this, ThirdActivity.class); 
       startActivity(intent); 
      } 
     }); 
    } 
    private View addNewLinearLayout(Context context) { 
     LinearLayout linearLayout = new LinearLayout(context); 
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
     linearLayout.setLayoutParams(params); 
     linearLayout.setBackgroundColor(getResources().getColor(R.color.black)); 
     // Do something else here on your linear layout or to customize your linear layout 
     return linearLayout; 
    } 

} 

을 이것은 XML입니다 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/lay" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/nextActivity" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" 
     android:layout_marginTop="100dp" 
     /> 

</LinearLayout> 

하지만 내 레이아웃이 자신의 색상을 변경하지 않습니다. 왜?

+0

정확히 달성하고 싶은 것은 무엇이며, 적절한 XML 레이아웃을 만드는 것만으로는 어떨까요? 기본적으로 view.AddView()를 사용하여 뷰를 추가 할 수 있습니다. –

+0

LinearLayout의 새 객체를 만들고 LinearLayout의 부모에게 addView 메소드를 사용합니다. – DevfaR

+0

다른 액티비티에서이 메서드를 사용하고 싶기 때문에 메서드를 갖고 싶습니다. 이 레이아웃을 모든 레이아웃에 추가하는 글로벌 메서드가 있습니다 – user1302569

답변

2

이 같은 시도 : 주에서

private View _addNewLinearLayout(context) { 
    LinearLayout linearLayout = new LinearLayout(context); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
    linearLayout.setLayoutParams(params); 
    // Do something else here on your linear layout or to customize your linear layout 

    return linearLayout; 
} 

당신 이것을 다음과 같이 부를 수 있습니다 :

getView().addView(_addNewLinearLayout(context)); 
+0

, 레이아웃은 여전히 ​​동일합니다. 편집 내용을 확인하십시오. – user1302569

+0

당신은 방법으로 돌아 가야 할 것입니다. 다음과 같음 : return linearLayout; – lokoko

+0

높이를 MATCH_PARENT (으)로 변경할 수 있습니까? linearLayout에 내용이없고 높이가 WRAP_CONTENT이므로 아무 것도 표시하지 않습니다. – lokoko

1

쉽게이 같은 새로운 LinearLayout을 만들 수 있습니다

LinearLayout linLayout = new LinearLayout(this); 
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT); 

을 그리고 그것을 설정 :

setContentView(linLayout, layoutParams); 
1

이 샘플 방법을 사용합니다. 원하는 경우 매개 변수를 추가 할 수 있습니다. l_details가 다른 선형 레이아웃을 추가하려는 선형 레이아웃의 인스턴스

public LinearLayout addLinearLayout(Context context) { 
    LinearLayout newLayout = new LinearLayout(context); 

    //Add stuffs here, like LayoutParams 

    return newLayout; 
} 

yourLinearLayoutName.addView(addLinearLayout(yourClassName.this)); 
1
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View detailView = inflater.inflate(R.layout.yourNewLayout, l_details, false); 

.

관련 문제