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!
Bạn có thể thêm bố cục nơi bạn muốn thêm 'TextView' không? – Luksprog
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
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