2014-10-19 2 views
0

레이아웃에이 LinearLayout이 있습니다. XML 레이아웃 파일에 복사하면 3 개의 요소가있는 다른 라인이 생기고 모든 것이 예상대로 나타납니다. 이제는이 LinearLayout과 자식 요소를 프로그래밍 방식으로 추가하려고 시도하지만 다르게 그리고 완전히 틀린 것처럼 보입니다. 버튼의 너비가 적당하지만 높이가 너무 낮고 다른 두 요소가 잘못된 높이와 너비로 보이지 않습니다. 안드로이드 세트 레이아웃이 프로그래밍 방식으로 잘못된 디스플레이를 나타냅니다

레이아웃입니다 :

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <EditText 
      android:id="@+id/editTextValueComposition" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="0.94" 
      android:ems="10" 
      android:hint="@string/valueHint" 
      android:inputType="numberDecimal" > 
     </EditText> 

     <Spinner 
      android:id="@+id/compositionSelector" 
      android:layout_width="176dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.06" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="addComposition" 
      android:text="@string/add" /> 
    </LinearLayout> 

그리고 이것은 내 코드입니다 :

public void addComposition(View view) { 
     final float scale = getBaseContext().getResources().getDisplayMetrics().density; 
     LinearLayout linearLayout = new LinearLayout(this); 
     linearLayout.setOrientation(LinearLayout.HORIZONTAL); 
     linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 

     EditText valueEdit = new EditText(this); 
     valueEdit.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 0.94f)); 
     valueEdit.setHint(R.string.valueHint); 
     valueEdit.setEms(10); 
     linearLayout.addView(valueEdit); 

     Spinner compositionSelector = new Spinner(this); 
     compositionSelector.setLayoutParams(new TableLayout.LayoutParams(dpToPx(176), LayoutParams.WRAP_CONTENT, 0.06f)); 
     ArrayAdapter<CharSequence> adapterComp = ArrayAdapter.createFromResource(
          this, R.array.compositionTypes, 
          android.R.layout.simple_spinner_item); 
     adapterComp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     compositionSelector.setAdapter(adapterComp); 
     linearLayout.addView(compositionSelector); 

     Button addCompoButton = new Button(this); 
     addCompoButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     addCompoButton.setText(R.string.add); 
     addCompoButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       addItem(v); 
      } 
     }); 
     linearLayout.addView(addCompoButton); 

     LinearLayout addItemLayout = (LinearLayout) findViewById(R.id.screenAddItem); 
     int index = addItemLayout.indexOfChild(findViewById(R.id.button1)); 
     addItemLayout.addView(linearLayout, index); 
    } 

    public int dpToPx(int dp) { 
     DisplayMetrics displayMetrics = getBaseContext().getResources().getDisplayMetrics(); 
     int px = Math.round(dp * (displayMetrics.xdpi/DisplayMetrics.DENSITY_DEFAULT)); 
     return px; 
    } 

답변

1

시도가 너무 쉬웠다 대신 TableLayout.LayoutParams

+0

아 감사의 LinearLayout.LayoutParams을 사용합니다. 나는 TableLayout이 가중치를 설정하는 유일한 방법이라고 읽었다 : – DarsVaeda

관련 문제