2013-07-09 3 views
1

Android 애플리케이션에서 작업 중이며 해결할 수없는 몇 가지 문제가 있습니다.EditText를 동적으로 삽입 할 때 높이가있는 Android 문제

xml에서 RelativeLayout은 하나는 EditText이고 동적으로 변수 값에 따라 하나 또는 두 개의 EditText을 동적으로 삽입해야합니다.

문제

문제는 그 첫 번째 (XML 선언 한) 상기 하나 EditText를 삽입 할 때, 그것은, I는 그것의 끝을 확인하고, I없는 너무 높이를 얻을 수 있다는 것이다 왜 일어나는 지 이해할 수 없습니다.

activity.xml

<!-- Some other layouts --> 
<RelativeLayout 
    android:id="@+id/relativeLayoutLectura1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/linearLayoutMensajeLectura" 
    android:layout_marginLeft="25dp" 
    android:layout_marginRight="25dp" 
    android:layout_marginTop="10dp"> 

    <EditText 
     android:id="@+id/editTextLectura1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/introducir_lectura" 
     android:textSize="@dimen/textSize" /> 

</RelativeLayout> 
<!-- More layouts --> 

Activity.java

if(lecturas > 1) { 
    // Have to insert lecturas-1 EditText 
    RelativeLayout parentLayout = (RelativeLayout) 
    findViewById(R.id.relativeLayoutLectura1); 
    for(int lectura = 2; lectura < lecturas; lectura++) { 
     EditText editTextLectura = new EditText(this); 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
     LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
     if(lectura == 2) { 
      //Insert below the one created in xml 
      params.addRule(RelativeLayout.BELOW, R.id.editTextLectura1); 
     } else { 
      //Insert below the one created in previous iteration 
      params.addRule(RelativeLayout.BELOW, lectura-1); 
     } 
     editTextLectura.setLayoutParams(params); 
     editTextLectura.setId(lectura); 
     editTextLectura.setHint(R.string.introducir_lectura); 
     editTextLectura.setTextSize(R.dimen.textSize); 
     //Add to layout 
     parentLayout.addView(editTextLectura); 
    } 
} 

당신이 날 문제를 찾는 데 도움이 있습니다 : 여기

내 코드는 최신인가?이 xml로 작성된 것이 올바르게 표시되고 코드로 작성된 것이 올바른 위치에 설정됩니다.

+0

id가 relativeLayout 인 것은 혼란 스럽습니다. @ + id/linearLayoutLectura1 – ben75

+0

예, 'LinearLayout'을 사용하기 전에'RelativeLayout'으로 변경했을 때 ID를 잊어 버렸습니다. – Lyd

답변

0

해결책을 찾았습니다. 나도 XML에서 사용, 당신이 볼 수 있듯이,

<resources> 
    <!-- Other dimen --> 
    <dimen name="textSize">15dp</dimen> 
</resources> 

하고, 그것은 확인을 실행합니다 :

editTextLectura.setTextSize(R.dimen.textSize); 

textSize

이 DIMEN 파일에 선언 : 내가 사용

<EditText ... 
    android:textSize="@dimen/textSize" /> 

이유는 모르지만 R.dimen.textSize 대신 15dp을 입력하면 Activity 코드가됩니다.

누구든지 설명 할 수 있으면 기뻐할 것입니다.

+0

그것은 나에게도 일어나고있다! –

+1

문제는 크기가 아니라 크기 리소스의 ID입니다. 즉,'editTextLectura.setTextSize (getResources(). getDimension (R.dimen.textSize));'가 있어야합니다. – Adinia

관련 문제