2012-06-21 5 views
0

나는 동적으로 내가하나 더 동적 레이아웃 또는 XML 레이아웃

아래 표와 같은 xml 파일에 선언 모든게으로 같은 일을하고, 마찬가지로

public class DynamicLayoutActivity extends Activity 
{ 
     private LinearLayout linear_layout,linear_main; 
    private Button b1,b2,b3; 
    private LinearLayout.LayoutParams linear_layout_params,button_parameters; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     linear_main = new LinearLayout(this); 

     linear_layout = new LinearLayout(this); 
     linear_layout_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT); 
     linear_layout.setOrientation(LinearLayout.VERTICAL); 
     linear_layout.setGravity(android.view.Gravity.CENTER); 
     linear_layout.setLayoutParams(linear_layout_params); 
     linear_layout.setId(3); 

    //  button_parameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); 

     b1=new Button(this); 
     b1.setText("Button1"); 
     b1.setId(31); 
     b1.setGravity(android.view.Gravity.CENTER); 
//  b1.setLayoutParams(button_parameters); 

     b2=new Button(this); 
     b2.setText("Button1"); 
     b2.setId(32); 
     b2.setGravity(android.view.Gravity.CENTER); 
//  b2.setLayoutParams(button_parameters); 

     b3=new Button(this); 
     b3.setText("Button1"); 
     b3.setId(33); 
     b3.setGravity(android.view.Gravity.CENTER); 
//  b3.setLayoutParams(button_parameters); 

     linear_layout.addView(b1); 
     linear_layout.addView(b2); 
     linear_layout.addView(b3); 

     linear_main.addView(linear_layout); 

     setContentView(linear_main); 
    } 
} 

아래 표와 같은 뷰를 만드는 오전 인 가정하자

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 

    </LinearLayout> 

내가 선형 레이아웃에서 100 개 버튼이 할 수있는 최선의 방법이며, 이는 다음 위에서 언급 한 가정 메모리 점유 및 실행 시간에 관련된 최상의 성능을 가지고있는

시나리오를 지원하는 경우 그 시나리오가 가장 적합한 이유에 대해 설명해주십시오.

답변

1

나는 당신이 레이아웃을 & 컨트롤을 정의하는 경우 귀하의 질문에

에 대답하려고합니다 .. 내가 사전에 동적으로

덕분에 XML을 및 만들기보기 Infalting 의 사용을 이해할 수 있도록 xml 그런 다음 당신이보기를 수동으로 인스턴스화하는 것보다 조금 느릴 것이라고 생각하는 코드로 그들을 부 풀린다. 그러나 나는 우리가 차이를 만들 수 있다고 생각하지 않는다.

  • xml 기반 레이아웃을 사용하면 다음과 같은 장점이 있습니다.

1 그들은 우리가 다양한 화면 해상도와 방향을

2 (당신은 LDPI, MDPI, hdpi에, xhdpi와 같은 각각의 폴더에 각 프로파일에 대한 main.xml에 파일을 배치해야합니다)를 지원 할 수 있습니다 Java 코드를 건드리지 않고도 레이아웃을 쉽게 변경할 수 있습니다.

+0

답변 해 주셔서 감사합니다. 왜 XML에서 레이아웃 및 컨트롤을 팽창시키는 것이 수동으로 뷰를 인스턴스화하는 것보다 조금 느릴지 설명해 주시겠습니까? 이 becoz 나는 인스턴스를보기 XML 파일을 부풀게하는 것보다 더 많은 시간이 걸릴 것입니다 딜레마에 묻습니다. ( –

+0

xml을 부 풀릴 때 구문 분석하거나 XML을 통해 당신이 요청한 요소를 찾을 수있는 몇 가지 알고리즘이 있어야하기 때문에. –

+0

예 ur right XMLPullParser를 사용하여 레이아웃 XML 파일을 구문 분석합니다. –