2012-07-09 5 views
15

아마 모든 게시물과 문서를 읽었지만 여전히이 문제를 해결할 수 없습니다.addView가 작동하지 않습니다.

기존의 (실행중인) 레이아웃에보기를 추가하려면 addView() 메소드를 사용하고 싶지만 어떤 이유로 든 위선적으로 말합니다. 나는 이것이 쉽고 기본적인 것이어야하지만 아직도 그것을 할 수 없다는 것을 안다. 그래서 제발 도와주세요. 여기

는 코드입니다

LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout); 
     TextView text=new TextView(this); 
     text.setText("test"); 
     layout.addView(text); 

코드, 그리고 결과는 내가 XML 파일에 정의되어 있습니다 만 뷰를 표시 한 것입니다. 내가 추가 한이 새로운 견해가 없습니다. 디버깅 할 때이 추가 된보기를 추가 한 부모의 자식으로 표시되지만 표시되지는 않습니다.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
       android:id="@+id/mainLayout" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical" 
       android:background="@drawable/main1" > 
    <TextView android:id="@+id/app_title" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textColor="#FFF" 
       android:text="@string/app_title" 
       android:textSize="25dp" 
       android:gravity="center_horizontal"/> 
    <TextView android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="5dp" 
       android:text="@string/main_screen_counter_title" 
       android:textSize="15dp" 
       android:textColor="#FFF" 
       android:gravity="center_horizontal"/> 
    <TextView android:id="@+id/frontScreenCounter" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textColor="#FFF" 
       android:text="@string/reading" 
       android:textSize="33dp" 
       android:gravity="center_horizontal" /> 
    <GridView android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:columnWidth="90dp" 
    android:numColumns="3" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
    android:textColor="#888" 
/> 
    </LinearLayout> 

이 도와주세요 : 여기

는 main.xml에 있습니다. 이것은 나를 미치게 할 것이다!

+0

'TextView'를 추가 할 레이아웃을 추가 할 수 있습니까? – Luksprog

+0

Hey Luksprog, 왜냐하면 android : layout_height = "fill_parent"가 LinearLayout 속성에 있기 때문입니다. 이제 텍스트가 표시되었지만 화면의 맨 아래에 있습니다. 그것을 맨 위에 올려 놓는 방법? 당신이 나를 도왔습니다. 간접적이지만 여전히. Tnx 많이. – Majstor

+1

그 'TextView'를 특정 위치에 놓고 싶다면 새로운'View'를 넣을 위치 인 int를 취하는'addView' 메쏘드의 다른 풍미를 사용해야합니다 :'addView (text, 0); ' – Luksprog

답변

15

새로 추가 된보기에 을 지정하는 것을 잊었습니다.

LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout); 
    TextView text=new TextView(this); 
    text.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    text.setText("test"); 
    layout.addView(text); 

편집

@+id/gridview의 ID가 새보기를 추가 할 공간을 떠나, fill_parent의 레이아웃 높이 정의와 함께 GridView. 높이를 wrap_content으로 변경하면 문제가 해결 될 수 있습니다.

다른 사람들이 솔루션을 쉽게 확인할 수 있도록이 게시물에 내 의견을 추가하십시오.

+0

어떤 매개 변수를 지정해야합니까? 여백, 너비, 높이를 추가하려고 시도했지만 아무 것도 할 수 없습니다. – Majstor

+0

mainlayout.xml을 게시 할 수 있습니까? Arun의 코드가 나에게도 효과가 있기 때문에 – firefistace

+0

나는 여전히 당신의 코드를 시도했다. 부모의 자식이지만 표시되지 않습니다. 아이디어가 더 있으십니까? 너의 노력을 위해 Tnx. – Majstor

-1

기억이 안 나지만 레이아웃을 다시로드하는 데 "새로 고침"방법이있을 수 있습니다.

try layout.invalidate();

+0

아직 시도하지 않았습니다. 아이디어가 더 있으십니까? – Majstor

+0

@Majstor 최상위 레이아웃에서'Invalidate();'를 호출해야합니다.이 객체에서 호출 했습니까? – ForceMagic

+2

addView 통화가 이미 무효화되었습니다. –

1

나는 동일한 문제가있어서 그 이유를 찾는 데 여러 시간을 낭비했다.

내 잘못은 match_parent를 높이로 설정 한 CustomView를 추가한다는 것이 었습니다.

나는이 바보 짜증나는 실수를 한 사람에 귀중한 시간 :

TextView tv = new TextView(this); 
lytMarks.addView(tv); 


CustomView cv = new CustomView(this, someObject); 
lytMarks.addView(cv); // <-- This one had height = match_parent!! 


EditText et = new EditText(this); 
lytMarks.addView(et); 
0

귀하의 LinearLayout 내용이 부모의보기 이상을 절약 할 수 있도록 노력하겠습니다. 그래서 ScrollView를 사용해야합니다. 은 다음과 같습니다 :

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<LinearLayout 
    android:id="@+id/mainLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/main1" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/app_title" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="@string/app_title" 
     android:textColor="#FFF" 
     android:textSize="25dp" /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:gravity="center_horizontal" 
     android:text="@string/main_screen_counter_title" 
     android:textColor="#FFF" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/frontScreenCounter" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="@string/reading" 
     android:textColor="#FFF" 
     android:textSize="33dp" /> 

    <GridView 
     android:id="@+id/gridview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:columnWidth="90dp" 
     android:gravity="center" 
     android:horizontalSpacing="10dp" 
     android:numColumns="3" 
     android:stretchMode="columnWidth" 
     android:textColor="#888" 
     android:verticalSpacing="10dp" /> 
</LinearLayout> 
</ScrollView>