2012-07-09 30 views
15

Tôi đã đọc có lẽ tất cả các bài đăng và tài liệu nhưng tôi vẫn không thể giải quyết vấn đề này.addView không hoạt động

Tôi muốn sử dụng phương thức addView() để thêm chế độ xem vào bố cục hiện tại (đang chạy) nhưng vì một số lý do tôi không thể. Tôi biết rằng điều này phải dễ dàng và cơ bản nhưng tôi vẫn không thể làm điều đó. Vì vậy, hãy giúp tôi.

Đây là một mã số:

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

Đó là một mã số, và kết quả là tôi đã hiển thị chỉ xem được định nghĩa trong file xml. Không có chế độ xem mới này mà tôi đã thêm. Khi tôi gỡ lỗi, tôi thấy chế độ xem được thêm này dưới dạng phần tử con của phụ huynh mà tôi đã thêm nó nhưng nó không được hiển thị.

đây là main.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> 

Xin vui lòng giúp. Điều này sẽ thúc đẩy tôi hạt!

+0

Bạn có thể thêm bố cục nơi bạn muốn thêm 'TextView' không? – Luksprog

+0

Xin chào Luksprog, Đó là vì android: layout_height = "fill_parent" trong thuộc tính LinearLayout. Bây giờ tôi đã hiển thị văn bản nhưng ở cuối màn hình. Làm thế nào để đặt nó ở đầu trang? Bạn đã giúp tôi. Gián tiếp, nhưng vẫn còn. Tnx rất nhiều. – Majstor

+1

Nếu bạn muốn đặt 'TextView' ở một vị trí nhất định, bạn nên sử dụng một hương vị khác của phương thức' addView' lấy int, vị trí đặt 'View' mới, giống như sau:' addView (văn bản, 0); ' – Luksprog

Trả lời

15

Bạn quên chỉ định LayoutParameters cho chế độ xem mới được thêm vào.

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); 

EDIT

Các GridView với id của @+id/gridview được xác định với chiều cao bố trí của fill_parent, để lại cho bạn không gian để thêm một cái nhìn mới. Thay đổi chiều cao của nó thành wrap_content có thể giải quyết vấn đề của bạn.

Thêm nhận xét của tôi vào bài đăng này để giúp người khác dễ dàng xác minh giải pháp.

+0

Tôi nên chỉ định các thông số nào? Tôi đã thử thêm lề, chiều rộng, chiều cao nhưng vẫn không có gì .. – Majstor

+0

Bạn có thể đăng tệp mainlayout.xml của mình không? Bởi vì mã của Arun cũng làm việc cho tôi quá – firefistace

+0

Tôi đã thử mã của bạn và vẫn không có gì. Của nó như là một đứa trẻ của cha mẹ nhưng không được hiển thị. Có thêm ý tưởng? Tnx cho nỗ lực của bạn. – Majstor

-1

Tôi không thể nhớ, nhưng có thể có bất kỳ phương thức "làm mới" nào để tải lại bố cục.

Thử layout.invalidate();

+0

Đã thử, vẫn không có gì. Có thêm ý tưởng? – Majstor

+0

@Majstor 'Invalidate();' phải được gọi trên bố cục cấp cao nhất, bạn có gọi nó từ đối tượng đó không? – ForceMagic

+2

cuộc gọi addView đã vô hiệu hóa rồi. –

1

Tôi đã có cùng một vấn đề và lãng phí nhiều thời gian tìm lý do.

Lỗi của tôi là tôi đã thêm Chế độ xem tùy chỉnh có match_parent được đặt làm chiều cao.

tôi hy vọng sẽ tiết kiệm một số thời gian quý báu cho bất kỳ ai đã làm sai lầm gây phiền nhiễu ngớ ngẩn này :)

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

nội dung LinearLayout của bạn là hơn Xem của cha mẹ. vì vậy bạn cần sử dụng ScrollView. như sau:

<?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> 
Các vấn đề liên quan